I don’t know why it’s giving me an error on step 11 - it says that it should "apply_discount(74.5, 20.0) should return 59.6
When I do my formula using a calculator, it gives me 59.6, so I assume it’s some sort of rounding error in Python, but when I tried adding round(price, 1) before, after, and even into the formula, it didn’t work
Your code so far
def apply_discount(price, discount):
if not isinstance(price, int or float):
return('The price should be a number')
elif not isinstance(discount, int or float):
return('The discount should be a number')
elif price <= 0:
return('The price should be greater than 0')
elif discount < 0 or discount > 100:
return('The discount should be between 0 and 100')
else:
return price - (price * (discount / 100))
apply_discount(100,20)
apply_discount(200,50)
apply_discount(50,0)
apply_discount(74.5, 20.0)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:152.0) Gecko/20100101 Firefox/152.0
Challenge Information:
Build an Apply Discount Function - Build an Apply Discount Function
Good news: it’s not a rounding issue at all — your formula is actually fine! The problem is higher up, in your type checks.
Take a close look at this line:
if not isinstance(price, int or float):
The tricky part is int or float. Python doesn’t read that as “int or float” the way you’d expect — or is a boolean operator, so int or float just evaluates to int alone, and float gets ignored. That means this line is really only checking isinstance(price, int).
So when you call apply_discount(74.5, 20.0), the price 74.5 is a float, not an int — the check decides it’s “not a number”, and the function returns the error message instead of ever reaching your formula. That’s why you never see 59.6.
The fix: how does isinstance expect you to pass multiple types at once? Look up its second argument — there’s a way to group several types together so it accepts both int and float. (Same applies to your discount check.)
Once that’s sorted, your math will run and give you 59.6.