簡體   English   中英

如何修復“TypeError:不支持的操作數類型+:'NoneType'和'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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM