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

93,597

Solution 1

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))

Solution 2

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)

Solution 3

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)

Share:
93,597
user2101517
Author by

user2101517

Updated on July 09, 2022

Comments

  • user2101517
    user2101517 almost 2 years

    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")
    
  • user2101517
    user2101517 about 11 years
    Thank you very much for your help!