在循环中更改变量?

2024-09-30 04:27:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做一个循环,每次循环都会改变变量

list0 = []
list1 = []
list2 = []

value = 0

for _ in range(3):
    list_l = ("list" + str(value))
    list_l.append("Apple")
    value += 1
    print(list0)
    print(list1)
    print(list2)

获取错误

AttributeError: 'int' object has no attribute 'append'

Tags: inappleforvalue错误rangelistint
1条回答
网友
1楼 · 发布于 2024-09-30 04:27:44

这就是你要找的吗?
https://www.online-python.com/LzbkxdmNA8

list0 = []
list1 = []
list2 = []
LargeList = [list0,list1,list2]

value = 0

for _ in range(3):
    value+=1;
    LargeList[_].append("apple-"+str(value))

print(list0)
print(list1)
print(list2)

输出:

['apple-1']
['apple-2']
['apple-3']

相关问题 更多 >

    热门问题