简体   繁体   中英

list assignment index out of range. Can you help me ?

j=1
n=int(input())
flag=[]*29
a=[]*29
for i in range(len(a)):
    a[i]=1.0/(i+2)
for j in range(2**29):
    temp=j
    index=0
    sum=0
    while temp>0 and index<29:
        flag[index]=temp%2
        index=index+1
        sum+=(temp%2)*a[index]
        temp=temp/2
if sum==1:
    count=0
    index=index+1
    for i in range(index):
        if flag[i]==1:
            count+=1
    if count==n:
        for i in range(len(flag)):
            if flag[i]!=0:
                print(flag[i]+"/"+(i+2)+"")

log:IndexError: list assignment index out of range

I can not fix.list assignment index out of range. Can you help me ?

当i = max(index)时,调用flag [i]时,“ index = index + 1”会将其设置为30,超出范围

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