2024-09-28 23:30:39 发布
网友
因此,我使用thonny编写python代码。我不断地犯这个错误。(不支持+(“str”和“int”)的操作数类型) 如果有人能帮助我,我将不胜感激。祝你圣诞快乐
numbers = ["1", "2", "3", "4", "5", "6"] print(numbers) index = 0 for i in numbers: print(numbers[index] + 19) index = index + 1
因为您的列表包含数字作为字符串: 应该是这样的:
numbers = [1, 2, 3, 4, 5, 6] print(numbers) index = 0 for i in numbers: print(numbers[index] + 19) index = index + 1
您正在尝试将整数和字符串相加。那是行不通的。如果它们是整数,则应在列表中不加引号地存储它们
numbers = [1,2,3]
您还应该简化for循环
for
for i in numbers: print(i + 19)
Python自动迭代列表的内容,因此i将自动成为列表的每个元素,而无需做任何额外的事情,因此增加索引(或使用索引访问列表)是多余的
i
numbers = ["1", "2", "3", "4", "5", "6"] print(numbers) index = 0 for i in numbers: print(int(numbers[index]) + 19) index = index + 1
您正在尝试将字符串添加到整数。执行从字符串到整数的类型转换
因为您的列表包含数字作为字符串:
应该是这样的:
您正在尝试将整数和字符串相加。那是行不通的。如果它们是整数,则应在列表中不加引号地存储它们
numbers = [1,2,3]
您还应该简化
for
循环Python自动迭代列表的内容,因此
i
将自动成为列表的每个元素,而无需做任何额外的事情,因此增加索引(或使用索引访问列表)是多余的您正在尝试将字符串添加到整数。执行从字符串到整数的类型转换
相关问题 更多 >
编程相关推荐