Python Forum

Full Version: 'NoneType' error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, quite new to Python and would be happy for some help with an exercise I'm working on.
I'm trying to write a tip with $ and % signs and get answers without, then calculate tip as tip = dollars * percent.
The issue is that I'm getting the following error: "TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'".
My question is why do I get this? why dollars/percent are not defined as the answer for the questions?
Thank you!
def main():
    dollars = dollars_to_float(input("How much was the meal? ")) 
    percent = percent_to_float(input("What percentage would you like to tip? "))
    tip = dollars * percent
    print(f"Leave ${tip:.2f}")


def dollars_to_float(d):
    dollars = print(float(d.lstrip("$")))

def percent_to_float(p):
    percent = print(float(p.rstrip("%")))  


main()
Your functions don't return a value.
Also, note that print() returns None
so a line like this
dollars = print(float(d.lstrip("$")))
will assign None to name dollars and even if you return dollars you still have a problem.

Check also What is the purpose of the return statement? How is it different from printing?
I'd like to add:

Even if you correct what has already been noted, you're expecting too much of the 'user'.

Given the input prompts, one would enter, (let's say 25 bucks for the meal, with a 10 percent tip) 25 and 10. The way things are, that would make the tip $250 (tip = dollars * percent),because you're expecting the user to enter 0.1 rather than 10, even if the user entered 10% at the input (which is what you're expecting with the line p.rstrip("%"), that does not alter the math.
Your spelling is correct.
(Nov-24-2022, 09:30 AM)buran Wrote: [ -> ]Also, note that print() returns None
so a line like this
dollars = print(float(d.lstrip("$")))
will assign None to name dollars and even if you return dollars you still have a problem.

Check also What is the purpose of the return statement? How is it different from printing?

I see your point.
I tried a different approach and this time I get: "can't multiply sequence by non-int of type 'str' ".
I find it hard to understand how to get an output that is non string nor sequence.
def main():
    dollars = dollars_to_float(input("How much was the meal? ")) # Recall that input returns a str
    percent = percent_to_float(input("What percentage would you like to tip? "))
    tip = dollars * percent
    print(f"Leave ${tip:.2f}")


def dollars_to_float(d):
    return d.lstrip("$")
    int(d)
  

def percent_to_float(p):
    return p.rstrip("%")
    int(p)  


main()
Please can you post code within Python tags?

Remember that once a return statement is hit, execution goes back to the caller of the function. No code in the function after the return is executed.

Which editor are you using? Some of them give you warnings about things like this, so you notice.
[removed]

Sorry: I'm going to drop this as I think that too many replies by too many people will only confuse you.
(Nov-25-2022, 06:34 AM)ndc85430 Wrote: [ -> ]Please can you post code within Python tags?

Remember that once a return statement is hit, execution goes back to the caller of the function. No code in the function after the return is executed.

Which editor are you using? Some of them give you warnings about things like this, so you notice.

First of all thank you! Can you elaborate? I tried many different things but cannot figure out how to take the input of the questions and return a float that is not 'NoneType'. For now I don't even try to eliminate the dollar and percent signs. The code and the error are attached below.

Code:
def main():
    dollars = dollars_to_float(input("How much was the meal? ")) # Recall that input returns a str
    percent = percent_to_float(input("What percentage would you like to tip? "))
    tip = dollars * percent
    print(f"Leave ${tip:.2f}")


def dollars_to_float(d):
     float()
  

def percent_to_float(p):
    float() 


main()
The error:
Error:
tip = dollars * percent TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'
a function with no return defaults to return None
def func_no_return():
    pass

print(func_no_return())
Output:
None
Use return to actually return something from a function
def func_with_return():
    return "Please post all code, output and errors (in their entirety) between their respective tags!"

print(func_with_return())
Output:
Please post all code, output and errors (in their entirety) between their respective tags!

Also Please post all code, output and errors (in their entirety) between their respective tags!