How to fix "TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'"?
Asked Answered
G

5

20

I am unsure why I am getting this error

count=int(input ("How many donuts do you have?"))
if count <= 10:
    print ("number of donuts: " ) +str(count)
else:
    print ("Number of donuts: many")
Greatest answered 23/2, 2013 at 3:10 Comment(0)
T
27

In python3, print is a function that returns None. So, the line:

print ("number of donuts: " ) +str(count)

you have None + str(count).

What you probably want is to use string formatting:

print ("Number of donuts: {}".format(count))
Tobolsk answered 23/2, 2013 at 3:10 Comment(0)
D
9

Your parenthesis is in the wrong spot:

print ("number of donuts: " ) +str(count)
                            ^

Move it here:

print ("number of donuts: " + str(count))
                                        ^

Or just use a comma:

print("number of donuts:", count)
Deadbeat answered 23/2, 2013 at 3:11 Comment(0)
B
2

Now, with python3, you can go with f-Strings like:

print(f"number of donuts: {count}")
Babar answered 28/7, 2021 at 9:15 Comment(0)
B
1

In Python 3 print is no longer a statement. You want to do,

print( "number of donuts: " + str(count) ) 

instead of adding to print() return value (which is None)

Bodycheck answered 23/2, 2013 at 3:12 Comment(0)
G
0

In python3, you can use this:

count=int(input ("How many donuts do you have?"))
if count <= 10:
    print ("number of donuts: ", count)
    #print ("Number of donuts: {}".format(count))
    #print(f"number of donuts: {count}")
    #you can use any of the above 3 statements to solve your problem
else:
  print ("Number of donuts: many")
Gain answered 23/6, 2024 at 5:49 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Husking

© 2022 - 2025 — McMap. All rights reserved.