Build an Apply Discount Function - Build an Apply Discount Function

Tell us what’s happening:

My code isn’t passing the check for “The price should be a number” but I don’t know why? It also won’t pass for any of the steps after even though it returns the correct values.

Your code so far

def apply_discount(price, discount):
    print(price - price * (discount/100))

    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')  
    else:
        cal_dis = (discount/100) * price
        final_price = price - cal_dis   
apply_discount(100,20)  
apply_discount(200,50)
apply_discount(50,0)
apply_discount(150,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/147.0.0.0 Safari/537.36 Edg/147.0.0.0

Challenge Information:

Build an Apply Discount Function - Build an Apply Discount Function

Github Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-discount-calculator/695774002591bbc5f8cf3e53.md at main · freeCodeCamp/freeCodeCamp · GitHub

this is the first line of your function, what happens here when it is not a number? call your function to test with a frist argument that is not a number, like apply_discount("22", 3)

Hi @amandasuarez94,

Does it make sense use price and discount before you have determined if they are a valid int or float.

Also, please wrap your function calls in print() to see what your function returns in the console. Is your function returning final_price?

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

Happy coding!

Hi! Yes the console was correctly outputting the values, so it was working. Everything worked, it was just giving me an error when I clicked “Check code”

did you try calling your functions with arguments that are not numbers?

I changed it to this: 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')  

else:

    cal_dis = (discount/100) \* price

    final_price = price - cal_dis  

    return(final_price) 

apply_discount(100,20)

apply_discount(200,50)

apply_discount(50,0)

apply_discount(150,100)

apply_discount(74.5,20.0)

and my code passed but nothing was returned in the console so I’m confused.

Yep. I changed all of the returns into prints and then it finally outputted it, but then I also got type errors with my <= 0 line even though I didn’t previously and didn’t change anything about that line.

You’re not seeing anything in the console because you did not wrap your function calls in print() as asked.