Python stops without any error messages.

Asked 2 years ago, Updated 2 years ago, 49 views

If you write the code and run it, it just stops without any error messages.

But if you do it 10 times, it's done well about 5 times.

If the last 0 remaining output is displayed, it is successful.

import random
_min=1
_max=11
_rng=10

num_list=random.sample(range(_min,_max),_rng)

print ('Initial Input List=',num_list)
count=0
len_list=len(num_list)
print ('Initial input list length=',len_list)

res_list=[]
first_temp=num_list.pop(0)
res_list.append([first_temp]) #Put the first value
count+=1
#print ('Result list =',res_list after only initial value)

while len(num_list)!=0:
    k=num_list.pop(0)
    i=0
    print('%s left' %len(num_list))
    print ('Input value =',k)
    while i<len(res_list):
        if k>res_list[i][0]:
            i+=1
            count+=1
            print ('%sth check' %i)

        if i==len(res_list):
            if k-res_list[i-1][-1]==1:
                res_list[i-1].append(k)
                print ('Result value when overlapping with front =',res_list)
                count+=1
                break
            else:
                res_list.insert(i,[k])
                print ('Result value when not overlapping =',res_list)
                count+=1
                break
        else:
            if res_list[i][0]-k==1:
                res_list[i].insert(0,k)
                print (res_list of results when overlapping with 'back' =',res_list
                count+=1
                break
            elif i!=0:
                if k-res_list[i-1][-1]==1:
                    res_list[i-1].append(k)
                    print ('Result value when overlapping with front =',res_list)
                    count+=1
                    break
            else:
                res_list.insert(i,[k])
                print ('Result value when not overlapping =',res_list)
                count+=1
                break

    result=[]
    for z in range( len(res_list)):
        result.extend(res_list[z])

    print ('final result list = %s' %result)
    print ('Tries = %s\n'%count)

python error

2022-09-22 09:00

1 Answers

There's a hole in the roof.

        elif i!=0:
            if k-res_list[i-1][-1]==1:
            ....
            Else: # I think there should be something here.


2022-09-22 09:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.