【发布时间】:2017-04-05 01:34:36
【问题描述】:
我在以下代码中得到一个超出范围的错误列表分配索引:
n=input("How many numbers?\n")
print "Enter ",n," numbers..."
a=[]
for i in range(0,n):
a[i]=input()
elem=input("Enter the element to be searched: ")
for i in range(0,n):
if a[i]==elem:
flag=1
break
if flag==1:
print "Item is present in the list"
else:
print "Item is not present in the list"
【问题讨论】:
-
使用
a.append(),而不是a[i] =。 -
请发布堆栈跟踪。 Python 非常好,可以为您提供有关错误的详细信息……请先付款!
标签: python python-2.7 list