파이썬이 아무 오류 메세지 없이 멈춥니다.

조회수 2950회

코드를 작성했는데 실행하면 아무 오류 메세지 없이 그냥 멈춥니다.

그런데 10번 실행하면 다섯 번 정도는 잘 실행이 되네요.

마지막에 0개 남음이 출력되면 정상 실행된 겁니다.

import random
_min=1
_max=11
_rng=10

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

print('초기 입력 리스트=',num_list)
count=0
len_list=len(num_list)
print('초기입력리스트 길이=',len_list)

res_list=[]
first_temp=num_list.pop(0)
res_list.append([first_temp]) #처음 값 넣기
count+=1
#print('초기값만 넣은 후 결과리스트=',res_list)

while len(num_list)!=0:
    k=num_list.pop(0)
    i=0
    print('%s개 남음'%len(num_list))
    print('들어간 값은=',k)
    while i<len(res_list):
        if k>res_list[i][0]:
            i+=1
            count+=1
            print('%s번째 확인'%i)

        if i==len(res_list):
            if k-res_list[i-1][-1]==1:
                res_list[i-1].append(k)
                print('앞에와 겹칠때 결과값은=',res_list)
                count+=1
                break
            else:
                res_list.insert(i,[k])
                print('안겹칠때 결과값은=',res_list)
                count+=1
                break
        else:
            if res_list[i][0]-k==1:
                res_list[i].insert(0,k)
                print('뒤에와 겹칠때 결과값은=',res_list)
                count+=1
                break
            elif i!=0:
                if k-res_list[i-1][-1]==1:
                    res_list[i-1].append(k)
                    print('앞에와 겹칠때 결과값은=',res_list)
                    count+=1
                    break
            else:
                res_list.insert(i,[k])
                print('안겹칠때 결과값은=',res_list)
                count+=1
                break

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

    print('최종결과 리스트=%s'%result)
    print('시도 수=%s\n'%count)
  • (•́ ✖ •̀)
    알 수 없는 사용자
  • 뭐하는 프로그램인가요? 정영훈 2018.5.5 20:02

1 답변

  • 루프에 구멍이 있네요..

            elif i!=0:
                if k-res_list[i-1][-1]==1:
                ....
                else :   # 여기에 뭔가 있어야 될듯.
    
    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)