Python +;的操作数类型不受支持:';int';和';str';
我目前正在学习Python,所以我不知道发生了什么Python +;的操作数类型不受支持:';int';和';str';,python,python-3.x,list,int,Python,Python 3.x,List,Int,我目前正在学习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
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的数字时,它返回:
回溯(最近一次呼叫最后一次):
您试图连接字符串和整数,这是不正确的 将
打印(numlist.pop(2)+“已被删除”)
更改为以下任一项:
显式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)))
试试看
str_list=”“.join([str(ele)表示numlist中的ele])
此语句将以string
格式为您提供列表中的每个元素
print(“列表现在看起来像[{0}]”。格式(str_list))
以及
更改打印(numlist.pop(2)+“已被删除”)
为
print(“{0}已被删除”。格式(numlist.pop(2))
同样。str(numlist)
当然不会导致异常(即使它可能不是OP所追求的)。
print("{} has been removed".format(numlist.pop(2)))