Build an Apply Discount Function - Build an Apply Discount Function

Tell us what’s happening:

I just need assistance with steps 7 and 8. I am pretty stuck, and

def apply_discount (price, discount):

if not isinstance (price, (int or float)):
    return ('The price should be a number')
    
elif not isinstance(discount, (int or float)):
    return ('The discount should be a number')

elif price <= 0:
    return ('The price should be greater than 0')

elif discount < 0 or discount > 100:
    return ('The discount should be between 0 and 100')

Hmmm!!
I think I am overthinking

Your code so far

def apply_discount (price, discount):
    
    if not isinstance (price, (int or float)):
        return ('The price should be a number')
        
    elif not isinstance(discount, (int or float)):
        return ('The discount should be a number')

    elif price <= 0:
        return ('The price should be greater than 0')

    elif discount < 0 or discount > 100:
        return ('The discount should be between 0 and 100')




    



Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36

Challenge Information:

Build an Apply Discount Function - Build an Apply Discount Function

Welcome to the forum @Iceeman117

To help you debug, place the following print call after the function.

print(apply_discount(100, 20))

Happy coding

Wait, why would I use “print”? Wouldn’t I want to use “return”
exmple:

return (apply_discount(price, discount))

The print call will print the result of calling your function.

The return keyword is used inside a function. For debugging, placing the function call inside a print call prints the result to the console, so the return keyword is not used.

Make sure the print call is not indented. It should not appear in the body of the function.

The print shows a “none”

hmm I’m still confused I guess on what I am missing in understanding this hint??

def apply_discount (price, discount):

if not isinstance (price, (int or float)):
    return ('The price should be a number')
    
elif not isinstance(discount, (int or float)):
    return ('The discount should be a number')

elif price <= 0:
    return ('The price should be greater than 0')

elif discount < 0 or discount > 100:
    return ('The discount should be between 0 and 100')

and when applying

print(apply_discount(100, 20))

it prints out “none” on the terminal.

I guess my question is what is this supposed to show or help me understand. I am most likely overthinking this.

Hi @Iceeman117

If all the conditions for checking price and discount are not met, what do you want the function to do?

None means the function is not returning anything.

Happy coding

I appricate that you are helping think this.

Well I would want the function to return the final_price . So

final_price = price - discount
?

Becuase if now if this is the final price , how would I set up the format for the discount?

would it be discount is the price the presentage of a discount over 100? So

else:

discount = price * (discount/100)

final_price = price - discount

return (price)

it still returning an error??

Please post your full code.

Hmmm it seems right to me but I guess I just don’t see the issue:

def apply_discount (price, discount):

if not isinstance (price,(int or float)):

return (‘The price should be a number’)

elif not isinstance(discount, (int or float)):

return (‘The discount should be a number’)

elif price <= 0:

return (‘The price should be greater than 0’)

elif discount < 0 or discount > 100:

return (‘The discount should be between 0 and 100’)

else:

    discount = price \* (discount/100)

    final_price = price - discount

print (apply_discount)

Hi there,

Is this a valid function call based on the function definition?

What is your function returning?

In the future, when you enter a code block into a forum post, please precede it with three backticks to make it easier to read and test.

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

Happy coding!

Well no that wouldn’t be valid since I’m not trying to print the discount, I am trying to print the final_price…. Oh, so would it be

print (final_price)

so my code wou be this

def apply_discount (price, discount):
    
    if not isinstance (price,(int or float)):
        return ('The price should be a number')
        
    elif not isinstance(discount, (int or float)):
        return ('The discount should be a number')

    elif price <= 0:
        return ('The price should be greater than 0')

    elif discount < 0 or discount > 100:
        return ('The discount should be between 0 and 100')
    else:
        discount= price * (discount/100)
        final_price= price - discount

    print (final_price)

Right?

hmmm my terminal is not throwing an error but is blank?

would I need to define discount and price for it to return something?

Your function is still not returning anything. Printing something is not the same as returning something.

And how can you test to see if your function returns what is expected without a function call?

Nice progress — you’re close.

For steps 7–8, try this structure:

  1. Validate inputs first (price/discount types and ranges).

  2. Convert discount to decimal: discount_rate = discount / 100

  3. Calculate final price: discounted_price = price * (1 - discount_rate)

  4. Return discounted_price (optionally round to 2 decimals).

Also, in isinstance use a tuple of types, e.g. (int, float), not (int or float).

If you want, I can help you write a minimal clean version line by line.

It is great that you solved the challenge, but 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

Oohhhh, okay, I got it now. My code works now, thanks!

Can anyone help with how to get clean output after run pthon?

Welcome back @jamensah2 ,

Please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Help button image located on each challenge. This will automatically import your code in a readable format and pull in the challenge URL while still allowing you to ask any question about the challenge or your code.

Thank you.

Happy coding!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.