我现在正在学习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的数字时,它返回: 回溯(最近一次呼叫时间):
TypeError: unsupported operand type(s) for +: 'int' and 'str'
试试看
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)))
还有。
您试图连接字符串和整数,这是不正确的。
将
print(numlist.pop(2)+" has been removed")
更改为以下任何选项:显式
int
到str
转换:使用
,
而不是+
:字符串格式:
相关问题 更多 >
编程相关推荐