Build a Discount Calculator - Build a Discount Calculator

Tell us what’s happening:

I am unable to pass tests 3-6 on the website - though I do think I should “pass”, based on my local machine. What might I be doing wrong here?

Your code so far

def apply_discount (price, discount):
    if type(price) != int and type(price) != float:
        print("The price should be a number")  
    else:    
        if type(discount) != int and type(discount) != float:
            print("The discount should be a number")
        else:   
            if 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:
                price = price - ((price*discount)/100)
                return price

Alternate version of my code

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Challenge Information:

Build a Discount Calculator - Build a Discount Calculator

I managed to solve my issue.

After reading the questions more closely, the tests which I failed asked for a ***return *** of the strings . Instead, I was using the print function. After removing all the print functions within apply_discount function, I passed all the tests.

The working solution

removed by moderator

I’ve removed your solution code. You should only post a blurred working solution like this if you are seeking code feedback.

1 Like