Python 3.3 TypeError:不支持的操作数类型+:'NoneType'和'str'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15036594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 3.3 TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
提问by user2101517
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")
采纳答案by mgilson
In python3, printis a functionthat returns None. So, the line:
在python3,print是一个功能是返回None。所以,该行:
print ("number of donuts: " ) +str(count)
you have None + str(count).
你有None + str(count)。
What you probably want is to use string formatting:
您可能想要的是使用字符串格式:
print ("Number of donuts: {}".format(count))
回答by Blender
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)
回答by Arcturus
In Python 3 printis no longer a statement. You want to do,
在 Python 3 中打印不再是一个语句。你想做的,
print( "number of donuts: " + str(count) )
instead of adding to print() return value (which is None)
而不是添加到 print() 返回值(无)

