将数字添加到循环中的某些列表上

问题描述 投票:0回答:1

我想编写一个程序,该程序将获取10个数字并将其组织到树形列表中并显示其长度。我尝试了这段代码:

for i in range(9):
    number=str(input("enter your number"))
list97=[]
list98=[]
list99=[]
if number[1]=="7" :
    list97.append(number)
elif number[1]=="8":
    list98.append(number)
else:
    list99.append(number)
print("length 97 is " + str(len(list97))"
print("length 98 is " + str(len(list98))"
print("length is 99 " + str(len(list99))"```

and i get nothing in lists. 
python append
1个回答
0
投票

您已经将str输入更改为int并在for循环之前初始化列表,然后像这样追加:

list97=[]
list98=[]
list99=[]
for i in range(9):
    number=int(input("enter your number"))
if number == 7:
    list97.append(number)
elif number == 8:
    list98.append(number)
else:
    list99.append(number)
print("length 97 is " + str(len(list97)))
print("length 98 is " + str(len(list98)))
print("length is 99 " + str(len(list99)))

0
投票

尝试一下:

list97=[]
list98=[]
list99=[]
for i in range(9):
    number=str(input("enter your number"))
    if number=="7" :
        list97.append(number)
    elif number=="8":
        list98.append(number)
    else:
        list99.append(number)
print("length 97 is " + str(len(list97)))
print("length 98 is " + str(len(list98)))
print("length is 99 " + str(len(list99)))
© www.soinside.com 2019 - 2024. All rights reserved.