Build a Discount Calculator - Build a Discount Calculator

Tell us what’s happening:

I cannot get test 4 to work, I need guidance please. Thanks!
4. When

apply_discount

is called with a

discount

(second argument) that is not a number (

int

or

float

) it should return

The discount should be a number

.

Your code so far

def apply_discount(price, discount):
    if price != (int or float):
        return 'The price should be a number'
    elif price <= 0:
        return 'The price should be greater than 0'   
    elif discount != (int or float):
        return 'The discount should be a number' 
    
    

Your browser information:

UPreformatted textser Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.2 Safari/605.1.15

Challenge Information:

Build a Discount Calculator - Build a Discount Calculator

Welcome to the forum @roca8144

To help you debug, add the following print call after the function definition.

print(apply_discount(50,0))

Happy coding

is this how you check if a value is of a certain data type? you may want to review a couple of lessons before this, like Understanding Variables and Data Types could be appropriate

Will do! Thanks for the help

1 Like

I changed my code up a bit but when I add the print(apply_discount(50,0))
The only thing that prints is “The price should be a number”

def apply_discount(price, discount):
    if isinstance(price, (int, float)):
        return 'The price should be a number'
    elif price <= 0:
        return 'The price should be greater than 0'  
    elif isinstance(discount, (int, float)):
        return 'The discount should be a number' 

print(apply_discount(50,0))

Refer to what @ILM mentioned above about checking variable types.

when do you expect the condition to be True, when price is a number or when it’s not a number?

1 Like

The condition should be True when it’s not a number. I see what I did wrong. Thank you very much!

I can’t seem to get test 8, 10, 11. Please help!

def apply_discount(price, discount):
    if not isinstance(price, (int, float)):
        return 'The price should be a number'
    elif price <= 0:
        return 'The price should be greater than 0'  
    elif not isinstance(discount, (int, float)):
        return 'The discount should be a number' 
    elif discount < 0 or discount > 100:
        return 'The discount should be between 0 and 100'
    return(price - discount)
print(apply_discount(100, 20))
print(apply_discount(200, 50))
print(apply_discount(50, 0))
print(apply_discount(74.5, 20.0))

Blockquote

Never mind I got it!

2 Likes

awesome! good job in figuring it out!

1 Like

Yeah, i was printing the errors but not making a ‘return’ of them like the exercise was asking, thanks roca8144 for your post, help me figure it out

1 Like

I am also newbe here your 1st to 3rd arguments is correct but the problem is in the else statement you should return the solution

// else: return the price multiply by (100 minus discount) devided by 100

price = apply_discount(your arguments)

print the price

…….end……

1 Like