1

I'm trying to get it so my code will return just the integer of 2 in stead of 2.5464893461251985

def number_of_cookies(amount, height, radius):
    return(amount/10)/(radius*radius*3.14158*height)

print (number_of_cookies(40, 0.5, 1))
assert number_of_cookies(40, 0.5, 1) == 2
assert number_of_cookies(400, 0.5, 1) == 25
assert number_of_cookies(1200, 0.3, 1) == 127
Miraj50
  • 4,257
  • 1
  • 21
  • 34

2 Answers2

0
def number_of_cookies(amount, height, radius):
    return int((amount/10)/(radius*radius*3.14158*height))

Setting to int will avoid the float precision you are seeing

C.Nivs
  • 12,353
  • 2
  • 19
  • 44
0

There are a couple of ways to do this. If you want it to become an integer, you can use int:

def number_of_cookies(amount, height, radius):
    return int((amount / 10) / (radius * radius * 3.14158 * height))

If there is a specified number of decimal places you want to round it to, you can use round:

def number_of_cookies(amount, height, radius):
    return round((amount / 10) / (radius * radius * 3.14158 * height), 0)

Finally, if you want to round it down, you can do this:

import math
def number_of_cookies(amount, height, radius):
    return math.floor((amount / 10) / (radius * radius * 3.14158 * height))
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42