Build an Apply Discount Function - Build an Apply Discount Function

Tell us what’s happening:

I’m lost and don’t understand how to continue.
I don’t understand def and return enough to be able to go through with this code even though I reread the theory multiple times. The result gives me the numbers in the brackets, but not the actual price with applied discount.

Your code so far

discount = ('price*(discount/100)')
price = ('price - discount')

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 and discount>100:
        return 'The discount should be between 0 and 100'
    else:
          print(price, discount)   
    
apply_discount(100, 20)
apply_discount(200, 50)
apply_discount(50, 0)
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/149.0.0.0 Safari/537.36

Challenge Information:

Build an Apply Discount Function - Build an Apply Discount Function

GitHub Link: https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/blocks/lab-discount-calculator/695774002591bbc5f8cf3e53.md

Hey @Linbene

Can you please tell me why are you using and over here?

Also the calculating discount and price logic is correct over here but can I ask why you have done it over here? User story tells us the function should calculate the discount as a percentage of the price if our inputs are valid but I don’t see the above logic inside the function.

Hi @Linbene,

You pass price and discount values to the function parameters when you call the function.

Here you are trying to define price and discount in the global space, but you don’t have price defined when you try to assign to discount. Not only that, but you don’t know yet if price and discount are either an int or a float, nor do you know yet if price and discount values are within the expected range.

With that in mind, where do you think you should write the code that returns the final price less the discount?

Happy coding

I am using and because I need to determine N. 6 When apply_discount is called with a discount lower than 0 or greater than 100, it should return The discount should be between 0 and 100. Therefore If discount is less than zero (<0) or greater than 100 (discount >100), return the string I assigned to it.

Regarding the second part of the message: If price is x and discount is y, the formula is x = x - x (y/100). Lets imagine price = 100, and discount is 20%, We determine the later price by using the formula, meaning 100 - 100 * (20/100), therefore price with discount is 80

Isn’t price and discount automatically assigned when I use the function print (apply_discount (price, discount)) ?

The chains “if not insistance” are already checking if its integer/float or a falsy, no?

Shall I add fin_price = (price - price*(discount/100))?

can a number be at the same time smaller than 0 and bigger than 100?

When you are using and, you are telling your code that both expression should be true and if it’s not true then don’t pass. Is that what step is asking?

Your logic is correct but where should you apply this logic? Outside the function or inside the function?

check this user story out:
If both inputs are valid, the function should calculate the discount as a percentage of the price

oh my god, its OR. thank you! i’m being stupid

I have applied the logic inside the function but there is still some errors. It gives me the correct answer and the next line is None. What does that mean

Yes, if you call the function like this: print(apply_discount(100,80), as mentioned below.

Yes, your code is checking the parameters that are passed into the function. My comments were directed to the price and discount variables you were trying to assign to in the global space rather than inside the function itself.

You should return, not print, the final price, which should be assigned only after your code has checked to make sure the parameter values are in the expected range and are the expected type.

That’s where I am at the moment.

notice all those None in the terminal. You are missing the return

you have the same requirement of returning the final price, that you had for all the strings

I did it, thank you so much!!! But why return and print act so different in the function? On paper they are kinda doing the same thing.

they are not, one determines what the function returns when it is called, the other is writing something to the terminal for you to see

However your brain works (seeing whatever is wrong with the code and being able to guide a person through it), its incredible. I hope I will get there someday. Thank you so much for your time.

once you have done a first chunk of the curriculum, you can come to the forum and start helping people with their code for this basic section, so you can learn how to help others with practice