Build an Apply Discount Function - Build an Apply Discount Function

Tell us what’s happening:

I am getting all the correct output to the Console,
but only the first 2 Tests are being Checkmarked. All the rest are X’s
I am lost.

Your code so far

def apply_discount(price, discount):

    if not isinstance(price, (int, float)):
        print ("The price should be a number.")
        return

    if not isinstance(discount, (int, float)):
        print ("The discount should be a number.")
        return

    if price <= 0:
        print ("The price should be greater than 0.")
        return

    if discount < 0:
        print ("The discount should be greater than 0.")
        return

    if discount < 0 or discount > 100:
        print ("The discount should be between 0 and 100.")
        return

    else:
        cal_dis = (discount*price)/100

    final_price = price - cal_dis

    return print(final_price)

apply_discount("1",1)
apply_discount(1,"1")
apply_discount(0,10)
apply_discount(1,101)

apply_discount(100, 20)
apply_discount(200, 50)
apply_discount(50, 0)
apply_discount(50, 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

Challenge Information:

Build an Apply Discount Function - Build an Apply Discount Function

I think the requirement says to return a string. Please double check your code and return a string instead of using print function.

Using RETURN’s made a huge difference.

I now have all Test’s completed EXCEPT #6 (The discount should be between 0 and 100)

Not sure why that one failed.

Tell us what’s happening:

All tests have been met, EXCEPT #6
For some reason, I cannot get that one to pass.
(The discount should be between 0 and 100)

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, float)):
        return "The discount should be a number"

    if price <= 0:
        return "The price should be greater than 0"
        
    if discount < 0:
        return "The discount should be greater than 0"
        
    if (discount < 0 or discount > 100):
        return "The discount should be between 0 and 100"
        
    else:
        cal_dis = (discount*price)/100

    final_price = price - cal_dis
    return final_price

print (apply_discount("1", 1))
print (apply_discount(1, "1"))
print (apply_discount(0, 50))
print (apply_discount(1, 101))
print (apply_discount(100, 20))
print (apply_discount(200, 50))
print (apply_discount(50, 100))
print (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

Challenge Information:

Build an Apply Discount Function - Build an Apply Discount Function

I went ahead and combined your posts for you. In the future, just reply to the original thread to add further updates.

Which user story asked you to make this validation check?

Do you even need the else?

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