Build a Discount Calculator - Build a Discount Calculator

Tell us what’s happening:

i am failing some tests on this code, any idea why?

Your code so far

def apply_discount(price, discount):

    if isinstance(price, (int,float)):
        return "The price should be a number."

    if 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."

    discount_amount = price * discount / 100
    final_price = price - discount_amount

    return final_price

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

Challenge Information:

Build a Discount Calculator - Build a Discount Calculator

Try testing your function by calling it and printing the result.

  1. 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.

Test it like this:

print(apply_discount("not a number", 10))
1 Like

Thanks for the help!

The function seems to be working fine.
4 test cases seems to be failing over and over

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

make sure to confront what the function is returning vs what it should return, double check spacing and punctuation, the string must be exactly the same

1 Like

Here are some debugging steps you can follow. Focus on one test at a time:

  1. Are there any errors or messages in the console?
  2. What is the requirement of the failing test?
  3. Check the related User Story and ensure it’s followed precisely.
  4. What line of code implements this?
  5. What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)

If this does not help you solve the problem, please reply with answers to these questions.

1 Like

Was able to debug.

Thanks for the help!