Build an Apply Discount Function - Build an Apply Discount Function

Tell us what’s happening:

Hi, I can only get no. 1 and 2 out of the list. I am unsure where I am going wrong and if it is just my understanding of functions and parameters to be wrong. I had also used elif 4 times instead of once but it made no difference to my code. Please help and thank you.

Your code so far

price = 25
discount = 0

def apply_discount (price, discount):
    if type(price) is not int or type(price) is not float:
        print("The price should be a number")

    if type(discount) != int or float:
        print("The discount should be a number")

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

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

    elif discount == True and price == True:
        final_price = price * (discount / 100)
    
    else:
        print(final_price)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36

Challenge Information:

Build an Apply Discount Function - Build an Apply Discount Function

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-discount-calculator/695774002591bbc5f8cf3e53.md at main · freeCodeCamp/freeCodeCamp · GitHub

please keep in mind there is a difference between printing and returning from the function

It keeps saying:

When apply_discount is called with a price (first argument) that is not a number (int or float) it should return The price should be a number.

as I said, printing and returning are not the same thing, you are printing, not returning

Okay thank you that made sense. Is there any chance I could get some help on my ending part of the code as I need getting the message

apply_discount(100, 20) should return 80

I am unsure where I have went wrong here.

Here is my code,

def apply_discount (price, discount):

    if type(price) is not int and type(price) is not float:

        return("The price should be a number")




    elif type(discount) is not int and type(discount) is not float:

        return("The discount should be a number")




    elif price <= 0:

        return("The price should be greater than 0")




    elif 0 > discount or discount > 100:

        return("The discount should be between 0 and 100")




    elif discount == True and price == True:

        discount = discount // 100

        price = price - (price * discount)

    

    else:

        return (price)

what value do you expect discount to be? will it ever be True?

if it’s a boolean, what happens here

Well I thought earlier since I put


if type(price) is not int and type(price) is not float:

        return("The price should be a number")




    elif type(discount) is not int and type(discount) is not float:

        return("The discount should be a number")

that it make sense because if it is not an integer or a float then my program would know it needs to be, but reviewing that it actually makes no sense because of my random form of logic ??

I ended up putting
elif isinstance(discount(int, float)) and isinstance(price(int, float)):

but that dosent work either

and i put

elif (type(price) is int or float) and (type(discount) is int or float):

       discount = discount // 100

        price = price - (price * discount)

        return(price)

which allowed no. 9 and 10 to pass, but has an issue with 7,8 and 11, but that would mean theres an issue with my working out to get the price? but that dosent make any sense either

additionally is there any chance you could please explain the difference of isinstance() and type(), I have seen some answers with isinstance() instead and i keep getting confused between the two. I want to try to learn how to make effecient code as early as possible. Thank you for the help so far

the first part of your code was fine

the issue is when you write discount == True you are checking if discount is exactly the same as True, which will never be, because discount is a numger

so consider, here, is something left to check? do you need additional checks for discount and price?

anyway, for type and isinstance take a look at https://www.freecodecamp.org/learn/python-v9/lecture-understanding-variables-and-data-types/how-do-the-type-and-isinstance-functions-work

thank you, this actually took way too long to do