我想编写一个程序,该程序将获取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.
您已经将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)))
尝试一下:
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)))