【问题标题】:How do I add both a variable and a string into the same print statement? [duplicate]如何将变量和字符串添加到同一个打印语句中? [复制]
【发布时间】:2019-12-14 15:52:24
【问题描述】:

我正在尝试为我的朋友编写一个基本的 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

也许我只是不擅长这个。我看了另一个答案,说逗号分隔字符串和 var,但这也不起作用。

【问题讨论】:

  • Your elemental attack is '''+ elematk + '''.''') 不是 Python 中的有效代码块。
  • 哎呀,我是想先放一个打印标签

标签: python


【解决方案1】:

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

将其转换为字符串类型

尝试做类似的事情:

print('elemental attack is '''+ str(elematk)+ '''.''')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 2014-01-18
    • 2017-05-24
    • 2015-12-30
    • 2019-09-10
    相关资源
    最近更新 更多