Build a Discount Calculator - Build a Discount Calculator

Tell us what’s happening:

I could not figure out the price & discount arguments when both are string and the answer cannot show ‘should be a number’ suggestion

Your code so far

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

apply_discount(100,20)
apply_discount(200,50)
apply_discount(50,0)
apply_discount(300,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/131.0.0.0 Safari/537.36

Challenge Information:

Build a Discount Calculator - Build a Discount Calculator

The first line tries to calculate before checking if the inputs are valid, and one of your error messages is printed instead of returned.

1 Like

correct first line’s print to return. But still could not know how to check inputs if it is valid///

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

if not isinstance (discount, (int,float)):
    return "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:
    print(price-discount/100\*price)

apply_discount(100,20)
apply_discount(200,50)
apply_discount(50,0)
apply_discount(300,100)
apply_discount(74.5,20.0)
///

actually I have corrected as followings:

def apply_discount (price, discount):
    print (price-discount/100*price)
    
    if 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

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

You don’t need print call in your code. And also, now you missing if statement for a price .

1 Like