如何将变量和字符串同时添加到同一打印语句中? [重复]

问题描述 投票:0回答:1

我正在尝试为我的朋友编写一个基本的Python RPG,并且试图创建一个统计生成器。从我在课堂上学到的知识,我应该print('Your ability score in this is ' + var1 + '.')

这是我遇到错误的整个代码块。

Your elemental attack is '''+ elematk + '''.''')

我得到的错误是

  File "/Users/data censored/Desktop/Basic RPG.py", line 24, in <module>
    Your elemental attack is '''+ elematk + '''.''')
TypeError: can only concatenate str (not "int") to str

也许我对此很不好。我看了另一个答案,说逗号用逗号分隔字符串和变量,但这也不起作用。

python
1个回答
0
投票

您的elematk变量是整数,因此不能将其与字符串连接。您可以通过执行str(elematk)

将其转换为字符串类型

尝试做类似的事情:

print('elemental attack is '''+ str(elematk)+ '''.''')
© www.soinside.com 2019 - 2024. All rights reserved.