我编写了这个简单的代码并尝试在 Windows 10 CMD 中执行......并且它收到错误消息:
TypeError: Can only concatenate str (not "int") to str
代码:
userName = input('Name: ')
age = input('age: ')
factor = 2
finalAge = age + factor
print('In', factor, 'years you will be', finalAge, 'years old', userName+'!')
我在 Windows 10 中使用 Python 3.7.0
最佳答案
代码第 2 行中的 input() 命令会将用户提供的任何输入转换为字符串。因此,当您尝试将该 STRING 添加到数字( float 或整数;在您的情况下,您有一个整数,即因子 = 2)时,它不会(也不应该!)工作。
因此,要使 + 运算继续进行,+ 号左侧和右侧的数量必须具有相同类型(字符串或数字)
关于python - 类型错误 : Can only concatenate str (not "int") to str (simple Python programme),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52225721/