Hello, my code fails when running the tests, but when i enter my own values for price and discount, it works perfectly, can someone explain to me whats wrong?
This is my Code:
Your code so far
discount = 20
price = 50
discount_dollars = discount / 100 * price
final_price = price - discount_dollars
def apply_discount (price,discount):
price = price
discount = discount
if not isinstance (price, (int, float)):
print ("The price should be a number")
if not isinstance (discount, (int, float)):
print ("The discount should be a number")
if price <= 0:
print ("The price should be greater than 0")
if discount <= 0 or discount > 100:
print ("The discount should be between 0 and 100")
else:
print (final_price)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:147.0) Gecko/20100101 Firefox/147.0
Challenge Information:
Build a Discount Calculator - Build a Discount Calculator
None of your validations are actually inside the function. Remember, in Python, your indentation is very important.
Also, most of the user stories say, “should return”. Is that what you are doing?
You are also doing your calculations outside of the function.
And is this necessary?
When you test your function, that’s where you should print the function call and pass in values to the function’s parameters, like this: print(apply_discount(50, 20))
It’s not a good idea to rely on global variables. The tests will run repeatedly and change the value of discount and price.
Sorry to bother you again, but now I rewrote my code and everthing seems to work but the calculation doesnt come out right would you mind taking a short look if you see whats wrong
Heres my code
def apply_discount(price,discount):
if not isinstance(price, (int,float)):
return ("The price should be a number")
if not isinstance (discount, (int,float)):
return ("The discount should be a number")
if price <= 0:
return ("The price should be greater than 0")
if discount < 0 or discount > 100:
return ("The discount should be between 0 and 100")
final_price = price - (discount / 100 * price)
else:
return (final_price)
Edit: I moved the final price variable with the calculation under the else: and solved the problem thank you