🔍 문제
6개의 수를 입력하고, 프로그램으로 부터 부여받은 7개의 난수(6개+보너스 1개)와 를 당첨 여부 확인
🗝 사용함수
random.sample(range(a, b+1), c) : a와 b 사이의 c개
random.randint(a,b) : a와 b 사이 난수 1개
If a in b : a가 b에 들어 있으면 True
len(x) : x(자료구조)의 길이
🖥 실행
1)모듈 생성
import random
userNums = []; randNums = []; correctNums = []
bonusNum = 0
def setUserNums(n):
global userNums
userNums = n
def getUserNums():
return userNums
def setRandNums():
global randNums
randNums = random.sample(range(1,46), 6)
def getRandNums():
return randNums
def setBonusNum():
global bonusNum
while True:
bonusNum = random.randint(1,45)
if bonusNum not in randNums:
break
def getBosNum():
return bonusNum
def lottoResult():
global userNums
global randNums
global correctNums
global bonusNum
correctNums = []
for i in userNums:
if i in randNums:
correctNums.append(i)
if len(correctNums) == 6:
print('1등 당첨!')
print(f'번호: {correctNums}')
elif (len(correctNums) == 5) and (bonusNum in userNums):
print('2등 당첨!')
print(f'번호: {correctNums}, 보너스 번호: {bonusNum}')
elif len(correctNums) == 5:
print('3등 당첨!')
print(f'번호: {correctNums}')
elif len(correctNums) == 4:
print('4등 당첨!')
print(f'번호: {correctNums}')
elif len(correctNums) == 3:
print('5등 당첨!')
print(f'번호: {correctNums}')
else:
print('다음 기회에')
print(f'기계 번호: {randNums}')
print(f'보너스 번호: {bonusNum}')
print(f'선택 번호: {userNums}')
print(f'일치 번호: {correctNums}')
def startLotto():
n1 = int(input('번호(1~ 45) 입력: '))
n2 = int(input('번호(1~ 45) 입력: '))
n3 = int(input('번호(1~ 45) 입력: '))
n4 = int(input('번호(1~ 45) 입력: '))
n5 = int(input('번호(1~ 45) 입력: '))
n6 = int(input('번호(1~ 45) 입력: '))
selectNums = [n1, n2, n3, n4, n5, n6]
setUserNums(selectNums)
setRandNums()
setBonusNum()
lottoResult()
2)모듈 호출
import lottoGame2 as lt
lt.startLotto()
📝 결과물
'Coding test > Python 기초문제' 카테고리의 다른 글
[모듈] 조합(combination) (0) | 2022.04.29 |
---|---|
[모듈] 순열(permutation) (0) | 2022.04.29 |
[모듈] 물건 가격 계산기 (0) | 2022.04.29 |
[모듈] 성적 패스 확인 (0) | 2022.04.29 |
[함수] 등차수열, 등비수열 계산기 (0) | 2022.04.29 |