Build a Discount Calculator - Build a Discount Calculator

Tell us what’s happening:

I have double checked my code but it keeps saying “Failed:3. 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.

  • Failed: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.

  • Failed:5. When apply_discount is called with a price lower than or equal to 0, it should return The price should be greater than 0.

  • Failed: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.

  • Failed:7. apply_discount(100, 20) should return 80.

  • Failed:8. apply_discount(200, 50) should return 100.

  • Failed:9. apply_discount(50, 0) should return 50.

  • Failed:10. When apply_discount is called with a discount of 100, it should return 0.

  • Failed:11. apply_discount(74.5, 20.0) should return 59.6.“

Your code so far

def apply_discount (price, discount):
    if type(price) != int or 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 reater than 0')
    if discount < 0 or discount > 100:
        print('The discount should be between 0 and 100')
    if discount and price:
        price * discount
        print(price)
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/145.0.0.0 Safari/537.36 Edg/145.0.0.0

Challenge Information:

Build a Discount Calculator - Build a Discount Calculator

All the tests that fail tell you what the function should return

What does your function return ? It prints a lot but does not return anything

I tried changing it from print to return, should I do both?

Do the instructions tell you to print something?

They say to return, but when I changed it to return, it still said the same

I changed it to return again and now test three is valid, but 4 to 11 are still failed

and I ran the tests again and 3 is failed

now that you have changed to return, you need to fix this

when you use or, it will have separate expressions on each side, so (type(price) != int) or (float)

you need to have a full expression on each side of or

I changed this but it still fails the same tests.

I have changed it to be isinstance(price, (int, float) but it still fails the tests

I updated the code to 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 reater than 0')

if discount < 0 or discount > 100:

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

if discount and price:

    price * discount

    return (price)

apply_discount(74.5, 20.0)

but tests 7, 8, 9, 10 and 11 are still failed.

There is a typo here

Yes, I have noticed this and fixed it

I think that you didn’t really understand how to apply a discount. Here, you do :

So for apply_discount(74.5, 20.0), you will see 1490. This result seems a bit awkward, don’t you think ?

To apply a discount on a object, you need to remove the given percentage from the initial price :

apply_discount(74.5, 20.0) should return the result of this operation →74,5 - 20%

1 Like

Thanks. I changed it after I posted that to price = (discount/100) * price but that doesn’t work, so I will try what you suggested

I changed it to

if discount and price:

    discount = discount / 100 * price

    price = price - discount

    return price

    print(price)

and now only test 9 is failed. Everything else is correct

can you post your current full code please?

Here:

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

    if discount and price:

        discount = discount / 100 * price

        price = price - discount

        return price

        print(price)

apply_discount(50, 0)

I double-checked with an online calculator and I got this:

Which is exactly what the code does, and it returns the correct number, but it doesn’t say it’s correct

Hello? I’ve been stuck on this for a long time, and I have not made any progress. I think the problem is the Python is trying to divide 0 by 100 and is having trouble. Anyone know how to fix this?