Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'NoneType' error
#1
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()
Yoriz write Nov-24-2022, 07:11 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Your functions don't return a value.
ndc85430 likes this post
Reply
#3
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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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.
ibreeden likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
Your spelling is correct.
Reply
#6
(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()
Yoriz write Nov-25-2022, 08:34 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#7
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.
buran likes this post
Reply
#8
[removed]

Sorry: I'm going to drop this as I think that too many replies by too many people will only confuse you.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#9
(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'
Yoriz write Nov-26-2022, 08:33 PM:

Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#10
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,088 Aug-17-2021, 05:24 PM
Last Post: Franky77
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 10,374 Apr-29-2020, 06:41 AM
Last Post: buran
  Error TypeError: '<=' not supported between instances of 'int' and 'NoneType' kaledhananjay 1 21,442 May-10-2018, 06:39 AM
Last Post: j.crater
  'NoneType' object has no attribute error, when using Kivy Screens chelovek 3 10,880 Feb-05-2017, 05:37 AM
Last Post: chelovek
  Regex error: 'NoneType' object has no attribute 'group' DBS 8 26,082 Oct-31-2016, 06:36 PM
Last Post: DBS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020