不支持的操作数types为+:'int'和'str'
我目前正在学习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))
当我运行该程序时,inputnum1,num2和num3的数字,它将返回:Traceback(最近一次调用的最后一个):
TypeError: unsupported operand type(s) for +: 'int' and 'str'
你试图连接一个string和一个整数,这是不正确的。
将print(numlist.pop(2)+" has been removed")
更改为以下任一项:
显式的int
到str
转换:
print(str(numlist.pop(2)) + " has been removed")
使用,
而不是+
:
print(numlist.pop(2), "has been removed")
string格式:
print("{} has been removed".format(numlist.pop(2)))
尝试,
str_list = " ".join([str(ele) for ele in numlist])
这个语句将以string
格式给你列表的每个元素
print("The list now looks like [{0}]".format(str_list))
和,
更改print(numlist.pop(2)+" has been removed")
print("{0} has been removed".format(numlist.pop(2)))
以及。