如何修复“TypeError:+ 不支持的操作数类型:'NoneType' 和 'str'”?

问题描述 投票:0回答:5

我不确定为什么会出现此错误

count=int(input ("How many donuts do you have?"))
if count <= 10:
    print ("number of donuts: " ) +str(count)
else:
    print ("Number of donuts: many")
python python-3.x string input typeerror
5个回答
27
投票

在python3中,

print
是一个返回None
函数
。 所以,这一行:

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

你有

None + str(count)

您可能想要的是使用字符串格式:

print ("Number of donuts: {}".format(count))

9
投票

你的括号位置错误:

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

移到这里:

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

或者只使用逗号:

print("number of donuts:", count)

2
投票

现在,使用 python3,您可以使用

f-Strings
,例如:

print(f"number of donuts: {count}")

1
投票

在 Python 3 中,print 不再是一个语句。你想做的事,

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

而不是添加到 print() 返回值(即 None)


0
投票

在python3中你可以使用这个

count=int(input("你有多少个甜甜圈?")) 如果数<= 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")

© www.soinside.com 2019 - 2024. All rights reserved.