【发布时间】:2019-02-13 00:24:08
【问题描述】:
当我在行中输入我的变量时,我得到了 TypeError。
num = int(input("Enter a number"))
print("You are " + num * 7 + " dog years old")
我希望它只接受输入,然后将其乘以 7 并给出最终数字,但我只是得到了错误。我几乎不了解 str 和 int,因此对它们的解释也会有所帮助。
【问题讨论】:
-
您不能连接字符串和整数。请参阅字符串的文档。 docs.python.org/3/library/stdtypes.html#str我想你正在寻找类似的东西:
print("You are " + str(num * 7) + " dog years old") -
如果你使用的是最近的python,你可以做 print(f"You are {num*7} dog years old")
标签: python python-3.x string exception input