![](/img/trans.png)
[英]How to fix: TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
[英]How to fix “TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'”?
我不確定為什么會收到此錯誤
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)
在 Python 3 中打印不再是一個語句。 你想做的,
print( "number of donuts: " + str(count) )
而不是添加到 print() 返回值(無)
現在,使用 python3,你可以使用f-Strings
如:
print(f"number of donuts: {count}")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.