Build a Discount Calculator - Build a Discount Calculator

Tell us what’s happening:

I am following the directions as far as I understand and am not sure where I am going wrong. Any help you be appreciated. Thank you in advance.

Your code so far

def apply_discount(price, discount):
    if price not int or float:
        return 'The price should be a number'
    if discount is not int or float:
        return 'The discount should be a number'
    if price <=0:
        return 'The price should be greater than 0'
    if discount is <1:
        return 'The discount should be between 0 and 100'

Your browser information:

User 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

Is there something specific you have a question about?

You cannot use or like this. Each expression is evaluated independently.

if (price not int) or (float):

So you are never checking if price is a float, you are just checking “float” which isn’t meaningful.

This also is not the way to check if a variable is an int. Do a web search for “Python check data type” or “python isinstance” to find out how.

1 Like

Okay I will check that out and correct the code. Thank you for the help.

Tell us what’s happening:

I was able to get the test for steps 1 and 2 to go through successfully. However from there the tests are failing. Any guidance would be appreciated and I have tried everything that I know how to do. Thank you in advance.

Your code so far

def apply_discount(price, discount):
    if not isinstance(price, number):
        return 'The price should be a number'
    if not isinstance(discount, number):
        return 'The discount should be a number'
    if price <=0:
        return 'The price should be greater than 0'
    if discount <1:
        return 'The discount should be between 0 and 100'

Your browser information:

User 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

You can’t use number in isinstance(), you have to use isinstance(price,(int,float) . If you want to use number you need to import it.

also consider what would your code do with a discount of 101

That makes sense. Okay. Thank you

I figured out my error there. Now I am struggling to use the apply_discount for the numbers it wants me to run. I don’t have any errors code though.

please always show your updated code when you need more help

Tell us what’s happening:

Hello. I have fixed the custom function and I have no error codes but when I call the function it isn’t returning anything. Please help.

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 or discount >100:
        return 'The discount should be between 0 and 100'
apply_discount(100, 20)
apply_discount(200, 50)
apply_discount(50, 0)
apply_discount(100, 0)
apply_discount(74.5, 20.0)


Your browser information:

User 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

Sorry im new to this and learning. I posted the code I have. Thank you for the clarification.

I have merged your two topics, please do not open multiple topics for the same challenge

what should your function do once the two values price and discount have been validated? and what part of your code is doing that?

I looked at your code and here are the following:

  • You are missing the formula for calculating the discount
  • That formula to calculate the discount will be inside an else statement after your last if statement
  • You then print what you need to print for the else statement and also return the parameter that calculates the final price in the else statement block
1 Like

This was very helpful thanks!!