我的程序在将条件中的列表追加到新列表中时遇到问题。谁能发现我的虫子?非常感谢你。该程序成功运行输出,只是将值附加到新列表中。在输出中,它仅打印一个空列表。
"""Take a list, say for example this one:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
and write a program that prints out all the
elements of the list that are less than 5.
Extras:
1. Instead of printing the elements one by one,
make a new list that has all the elements less
than 5 from this list in it and print out this
new list.
2. Ask the user for a number and return a list
that contains only elements from the original list
a that are smaller than the number given by the user"""
a=[1,2,3,3,4,4,4,5,5,7,8,8,12,15,20]
print "Here is the current list: \n"
print a
length = len(a)
print "Numbers less than 5 in list: \n"
for i in range(0,length):
if a[i]<5:
print "%d " %a[i]
new_list=[]
for n in range(0,length):
if a[i]<5:
new_list.append(n)
print "Here are all the numbers in a new list: \n"
print new_list
find_num = int(raw_input("Enter a number to find numbers less than it\n"))
for c in range(0, length):
if a[c]<find_num:
print "%d, " %a[c]
for n in range(0,length):
if a[i]<5:
您看到问题了吗?提示:n
,i
…