Build a Discount Calculator - Build a Discount Calculator

Tell us what’s happening:

Tests 1 to 7 pass, but then the following ones, although the results I get in the console are mathematically correct. Can you please tell me what I do wrong? Maybe I do not understand the assignment?

Your code so far

def apply_discount(price, discount):
    if not isinstance(price, (int, float)):
        return "The price should be a number"
    elif not isinstance(discount, (int, 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 - discount) / price * 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))





    

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3.1 Safari/605.1.15

Challenge Information:

Build a Discount Calculator - Build a Discount Calculator

What the second argument - discount is expressing?

are you sure this is the formula to calculate the discount percentage?

This should not be passing.

** start of main.py **

code removed by moderator

** end of main.py **

This is my code, you can use it as a reference.

hi @Laura_Xu

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Yes, sorry, I missed that point. Here is my new code, all tests pass except Nr 9, and I am really wondering why in the console I get 0 as a result instead of 50. Can you pls guide me?

def apply_discount(price, discount):
    if not isinstance(price, (int, float)):
        return "The price should be a number"
    elif not isinstance(discount, (int, 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 - (discount / 100 * price)

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))

which call is resulting in 0?

you have 5 calls

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))

and 5 outputs:

80
100
The discount should be between 0 and 100
0
59.6

that means that the outputs are

print(apply_discount(100, 20)) # 80
print(apply_discount(200, 50)) # 100
print(apply_discount(50, 0)) # The discount should be between 0 and 100
print(apply_discount(100, 100)) # 0
print(apply_discount(74.5, 20.0)) # 59.6

which one is giving the wrong output?