简体   繁体   中英

Python List Indexing Issue

Please Tell me what's going wrong in my codes, it's showing "d[j+1]=int(n) Index Error: list assignment index out of range"... see that line 13

d only has one value, which is 10 . You are trying to take the index 0+1 , ie the second element in d , even though this second element doesn't exist.

Perhaps d.append(j) is what you're looking for?

If I understood correctly what you want, it would have to be like this:

b=3
print(b)
d=[0]
a=[5]
i=0
j=0
c=0
while(c<d[0]):
    print("Enter Values: ")
    m,n=(input(" ").split(','))
    d[j]=int(m)
    m=0
    d[j+1]=int(n)
    n=0
    a[i]=d[j]+d[j+1]
    a[i+1]

print("Answer: "+ str(a))

Result:

3
Answer: [5]

Since c and d are equal to 0 , while passes through and prints the a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM