+不支持的操作数类型:'int'和'str'[重复]
问题内容:
这个问题已经在这里有了答案 :
如何连接str和int对象? (4个答案)
4年前关闭。
我目前正在学习Python,所以不知道发生了什么。
num1 = int(input("What is your first number? "))
num2 = int(input("What is your second number? "))
num3 = int(input("What is your third number? "))
numlist = [num1, num2, num3]
print(numlist)
print("Now I will remove the 3rd number")
print(numlist.pop(2) + " has been removed")
print("The list now looks like " + str(numlist))
当我运行该程序时,输入num1,num2和num3的数字,它将返回以下内容:Traceback(最近一次调用为last):
TypeError: unsupported operand type(s) for +: 'int' and 'str'
问题答案:
您正在尝试连接一个字符串和一个整数,这是不正确的。
更改print(numlist.pop(2)+" has been removed")
为以下任何一项:
明确int
到str
转换:
print(str(numlist.pop(2)) + " has been removed")
使用,
代替+
:
print(numlist.pop(2), "has been removed")
字符串格式:
print("{} has been removed".format(numlist.pop(2)))