我从一些基本的东西开始,了解如何制作一个基于文本的游戏。在我的集成开发环境中创建.py文件后,我打开终端并使用bash打开.py文件。
hpGanon = 10
damage = input("How much damage did you do with the Master Sword?")
hpGanon = hpGanon - damage
print("Ganon now has " + hpGanon + "hit points.")在我想要它打印的最后,bash告诉我它不能连接'str‘和'int’对象。我试着遵循下面的帖子,Python: TypeError: cannot concatenate 'str' and 'int' objects,但我没有得到我想要的结果。
我只想让它说:"Ganon现在有x个生命点了。“有什么想法吗?
发布于 2017-06-27 07:11:34
我不能完全确定(我以前从来没有用Python编写过代码),但是hpGanon变量似乎是一个整数而不是字符串,所以它不能把这两者放在一起。要在Python语言中将整数转换为字符串(我在上面搜索过:P),您需要执行str(integer)。
因此,要在您的示例中实现这一点,请尝试如下所示:
print("Ganon now has " + str(hpGanon) + "hit points.")
发布于 2020-07-21 19:56:46
例如,您可以执行数学计算并将其转换为字符串
*a = x*3
str(a)*现在,如果您在print语句中调用它,它将会工作。
a = x*3
str(a)
print("3 x {} = {} ").format(x,a)https://stackoverflow.com/questions/44769909
复制相似问题