Python 3.3 TypeError: unsupported operand type(s)

2020-08-13 01:37发布

New to programming and 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")

3条回答
▲ chillily
2楼-- · 2020-08-13 01:51

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))
查看更多
beautiful°
3楼-- · 2020-08-13 01:52

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)
查看更多
beautiful°
4楼-- · 2020-08-13 02:06

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)

查看更多
登录 后发表回答