Build an Apply Discount Function - Build a Discount Calculator

Tell us what’s happening:

Hello! I am having an issue getting the equation to work on this one

Your code so far

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

price = 20
discount = 100

apply_discount(100, 200)
apply_discount(200, 50)
apply_discount(50, 0)
apply_discount(100, 100)
apply_discount(74.5, 20.0)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0

Challenge Information:

Build an Apply Discount Function - Build a Discount Calculator

Price and discount values are passed to the function when you call it, so there is no need to create global variables for those.

You are trying to print a call to apply_discount inside the function itself.

Instead, wrap each of the test function calls in the global space inside a print() so you can see what your function returns and compare to what the corresponding test expects.

Does this validation check meet the requirement of User Story #4?

Does this validation check meet the requirement of User Story #6?

print(apply_discount(50, 0))
print(apply_discount(100, 100))

What do you see in the console? Does it make sense to have a final price of 0 if the price is 50 and the discount is 0%? Or a final price of 100 if the price is 100 and the discount is 100%?

You need to rework your algorithm for the final price.

1 Like

Thank you! I see where I messed up on the story 4, looking at it is kind of a duh moment. This project is making me understand py more.

I’ve fixed the other issues but my results are coming up negative, any hints or ideas?

worked on the other issues, not fixed

you did not follow the advice about checking the returned value of the function, now you have removed the return instead, so what is your function returning?

1 Like

apologies on that, I reset the project after the last comment and restarted, I’m getting none after each test

that is what your function is returning, None

what is being returned inside the else?

1 Like

I see what you mean, I returned instead of printed and it’s no longer returning NONE, but I’m still getting negatives. Is there a way I can define the discount_amount without breaking the equation?

do you expect discount_amount to ever be greater than price?

1 Like

hahaaaaaa, once again a duh moment. Thank you for the help! I have a feeling this profession is going to continuously humble me but I enjoy the process

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.