新的節目和我不確定爲什麼我收到此錯誤的Python 3.3類型錯誤:不支持的操作數類型(S)爲+:「NoneType」和「海峽」
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 3.3類型錯誤:不支持的操作數類型(S)爲+:「NoneType」和「海峽」
count=int(input ("How many donuts do you have?"))
if count <= 10:
print ("number of donuts: ") +str(count)
else:
print ("Number of donuts: many")
在python3,print
是功能返回None
。所以,行:
print ("number of donuts: ") +str(count)
你有None + str(count)
。
你可能想要的是使用字符串格式化:
print ("Number of donuts: {}".format(count))
你的括號是在錯誤的地方:這裏
print ("number of donuts: ") +str(count)
^
移動它:
print ("number of donuts: " + str(count))
^
或者只是使用逗號:
print("number of donuts:", count)
非常感謝您的幫助! – user2101517 2013-02-23 03:15:59
這適用於我。謝謝..!! – 2016-09-29 10:51:04
在Python 3中print不再是一個聲明。你想要做的,
print("number of donuts: " + str(count))
,而不是添加到打印()的返回值(這是無)
+1格式化 – Ric 2013-02-23 03:13:59
非常感謝您的幫助! – user2101517 2013-02-23 03:15:32