歡迎選擇我的課程,讓我們一起見證您的進(jìn)步~~
This is the result of traversing i!=j. You can see that when i=1,j=3 is traversed, the conditions are met and count+1 is reached. At this time, 1 and 3 are removed from list_c Deleted.
Then because two digits were deleted from the front of list_c, the array elements are moved forward by two digits, and j in the next cycle is equal to 6. Then i!=j at this time, and count has become 1 due to the previous cycle. , so the deletion will still be performed this time.
And since 1 has been deleted, list_c.remove(1) will report an error.
a=18
list_c = list(range(1,a+1))
max = list_c[-1] + list_c[-2]
k=[]
for i in range(100):
if i**2<=max:
k.append(i**2)
count=0
for i in range(a-1,-1,-1):
if list_c[i] == 0:
continue
for j in range(0,a):
if list_c[j]==0:
continue
if list_c[i] != list_c[j]:
if list_c[i]+list_c[j] in k:
print("%s 和 %s配對" % (list_c[i],list_c[j]))
list_c[i]=0
list_c[j]=0
break
Brother, I suggest you use a better IDLE. At least it will prompt you if you make a mistake in writing the code. Otherwise, this kind of error would be so boring