Build a Discount Calculator - Build a Discount Calculator

Tell us what’s happening:

My program is unable to complete the 5th and 6th objective. I have switched the print statements to return and the final price doesn’t return which is really frustrating. How can I fix this?

Your code so far

def apply_discount(price, discount):

    priceClass = isinstance(price, (int,float))
    if priceClass != True:
        return("The price should be a number")
    discountClass = isinstance(discount, (int, float))
    if discountClass != True:
        return ("The discount should be a number")
# Calculation
    discountedPrice = discount * price/100
    finalPrice = (price - discountedPrice) 
     

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

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


print(apply_discount(50,0))

Your browser information:

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

Challenge Information:

Build a Discount Calculator - Build a Discount Calculator

please share the code with the return statements

removed by moderator

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

     
    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"
            
    return finalPrice

    print(apply_discount(50,0))

It doesn’t return anything in the console. I don’t know why.

I edited this. I realized the last print was indented in the function. this is the new code

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"
            
    return finalPrice

print(apply_discount(50,0))

It doesn’t print the final price which I need to complete the 8th objective. How can I fix this?

What output is shown?

What does that tell you about what’s happening when the code runs?

Which if statement is triggered?

OMG FINALLY the discount is supposed to be within the range of 0-100 . my code doesnt return finalprice if the discount = 0 so i changed it to <0

1 Like