Build an Apply Discount Function - Build an Apply Discount Function

Tell us what’s happening:

The function seems to work just fine; it prints everything as it should be in the terminal, but only the first 2 requirements in the “Tests” section are validated.

Your code so far

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

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.3 Safari/605.1.15

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

Welcome back @Legion24,

Are you asked to return validation messages or print them? Is your function returning anything?

To see what your function returns in the console, wrap your function calls in print.

Happy coding

Thanks a lot, I realised i dint need to return print and could just use return with parentheses and then the string. Now all the test have a tick exept the 5th, also I see nothing printed on the terminal. Below is my code:

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 ')

if 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’)

else:

    discount_amount = price \* (discount/100)

    final_price = price - discount_amount

return final_price

apply_discount(100, 20)

apply_discount(200, 50)

apply_discount(50, 0)

apply_discount(800, 100)

apply_discount(-50, 50)

apply_discount(74.5, 20.0)

apply_discount(200, 200)

Look closely. Does you message match exactly to the instructions?

In the future, when you post your updated code, please format it as follows:

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor: