I’m struggling with this one a little as well. My code technically meets each objective, since I can get each of the price & discount values calculated as designed, though when I input a string for either, that’s the only part that doesn’t pass the required objectives, though it does say “This should be a number” for each, but it also returns “None” afterward as well. I have looked through this thread and a couple others and I’m wondering if this leeson specifically wants us to use “type()” instead of “isinstance()” since I don’t believe we’ve learned that in the course yet.
Here’s my code:
def apply_discount(price,discount):
if not (isinstance(price, int) or isinstance(price, float)):
print("The price should be a number")
elif not (isinstance(discount, int) or isinstance(discount, float)):
print("The discount should be a number")
elif price <= 0:
print("The price should be greater than 0")
elif discount < 0 or discount > 100:
print("The discount should be between 0 and 100")
else:
return (price - (price * (discount / 100)))
print(apply_discount(100, 20))
print(apply_discount(200, 50))
print(apply_discount(50, 0))
print(apply_discount(100,100))
print(apply_discount(74.5, 20.0))
print(apply_discount("five", 20.0))
print(apply_discount(74.5, "twenty"))
print(apply_discount(0, 10))
print(apply_discount(74.5, 101))
And here’s the associated outputs:
80
100
50
0
59.6
The price should be a number
None
The discount should be a number
None
The price should be greater than 0
None
The discount should be between 0 and 100
None
Edit: I added in the last 2 “print()” functions to show that my code works for steps 3-6, though none of them are checked off the list as completed objectives.