Search

이차반복4_문제03_로또정렬

대분류
STEP07 이차반복문
소분류
이차반복문4_문제

영상

문제

''' [문제] 1~45사이의 랜덤값 6개를 lotto에 저장하려 한다. 단, 중복되는 숫자는 없어야 하며, 내림차순 정렬 후 출력하시오. [예시] [40, 38, 27, 26, 18, 5] ''' lotto = []
Python
복사

해설

''' [문제] 1~45사이의 랜덤값 6개를 lotto에 저장하려 한다. 단, 중복되는 숫자는 없어야 하며, 내림차순 정렬 후 출력하시오. [예시] [40, 38, 27, 26, 18, 5] ''' import random lotto = [] i = 0 while i < 6: r = random.randint(1, 45) check = False j = 0 while j < i: if r == lotto[j]: check = True break j += 1 if check == False: lotto.append(r) i += 1 i = 0 while i < len(lotto): max = lotto[i] maxIndex = i j = i while j < len(lotto): if max < lotto[j]: max = lotto[j] maxIndex = j j += 1 temp = lotto[i] lotto[i] = lotto[maxIndex] lotto[maxIndex] = temp i += 1 print(lotto)
Python
복사