我有一个问题,关于下面这段代码:
if verb == "stoke":
if items["furnace"] >= 1:
print("going to stoke the furnace")
if items["coal"] >= 1:
print("successful!")
temperature += 250
print("the furnace is now " + (temperature) + "degrees!")
^this line is where the issue is occuring
else:
print("you can't")
else:
print("you have nothing to stoke")
产生的错误如下所示:
Traceback(most recent call last):
File "C:\Users\User\Documents\Python\smelting game 0.3.1 build
incomplete.py"
, line 227, in <module>
print("the furnace is now " + (temperature) + "degrees!")
TypeError: must be str, not int
我不确定问题出在哪里,因为我已经将名称从temp更改为temperature,并在temperature周围添加了括号,但仍然出现错误。
+
时,它期望2个字符串,并且如果没有得到2个字符串,则会失败。注意:print()
接受多个参数并自动调用这些参数的__str __()
,因此您可以执行print(“炉子现在为”,温度,“度!”)
而无需显式转换。 - AChampion