728x90
# 1. 모드 선택 (1.자동, 2.반자동, 3.수동)
# 2. 모드 별 번호 뽑기
# 2-1. 자동 모드 - 번호 6개 랜덤 추출
# 2-2. 반자동 모드 - 1~5개 번호 입력 받고 나머지 번호 5~1개 랜덤 추출
# 2-3. 수동 모드 - 6개 번호 입력 받기
# 3. 로또 번호 추첨
# 4. 당첨 결과 판정
# 5. 등수에 따른 당첨금 출력
import random
# 0. 입력 받는 경우 q를 누르면 프로그램을 종료
def quit(inputStr):
if inputStr == 'q':
exit("프로그램을 종료합니다.")
# 1. 모드 선택 (1.자동, 2.반자동, 3.수동)
def modeSelect():
while True:
print("*" * 50)
print("모드를 선택해 주세요.")
modeNumber = input("1.자동, 2.반자동, 3. 수동 => ")
quit(modeNumber)
# 입력된 값이 숫자가 아니면 모드 다시 입력 받기
if not modeNumber.isdigit():
print("1, 2, 3 중에 선택해 주세요.")
continue
modeNumber = int(modeNumber)
# 입력된 값이 숫자이지만 1, 2, 3 외의 값이 입력되면 다시 입력 받기
if modeNumber < 1 or modeNumber > 3 :
print("1, 2, 3 중에 선택해 주세요.")
continue
break
return modeNumber
# 2. 모드 별 번호 뽑기
def myNumber(modeNumber):
myNumberList = list()
# 2-1. 자동 모드 - 번호 6개 랜덤 추출
if modeNumber == 1:
myNumberList = random.sample(range(1, 46), 6)
# 2-2. 반자동 모드 - 1~5개 번호 입력 받고 나머지 번호 5~1개 랜덤 추출
elif modeNumber == 2:
# 번호 몇 개를 수동으로 입력할지 저장
while True:
print("반자동 모드입니다.")
inCount = input("번호 몇 개를 입력하시겠습니까? ")
quit(inCount)
# 입력된 값이 숫자가 아니면 모드 다시 입력 받기
if not inCount.isdigit():
print("1 ~ 5 중에 선택해 주세요.")
continue
inCount = int(inCount)
# 입력된 값이 숫자이지만 1 ~ 5 외의 값이 입력되면 다시 입력 받기
if inCount < 1 or inCount > 5:
print("1 ~ 5 중에 선택해 주세요.")
continue
break
# 수동으로 입력할 갯수만큼 번호 입력 받기
count = 1 # 입력받은 번호 갯수
while True:
number = input("{}개 숫자 중 {}번째 숫자 : ".format(inCount, count))
quit(number)
# 숫자가 입력되었는지 확인
if not number.isdigit():
print("1 ~ 45 사이 숫자를 입력하세요.")
continue
number = int(number)
# 숫자가 입력되었으면, 1 ~ 45 사이 숫자가 맞는지 확인
if number < 1 or number > 45:
print("1 ~ 45 사이 숫자를 입력하세요.")
continue
# 1~45 사이 숫자이지만 이전에 받은 번호와 중복되진 않는지 확인
if number in myNumberList:
print("번호가 중복되었습니다. 다시 입력해 주세요.")
continue
myNumberList.append(number)
if count == inCount: # 입력하려던 번호 갯수를 채우면 입력 종료
break
else:
count = count + 1
# 나머지 번호는 자동으로 추출
count = 1
while True:
number = random.randint(1, 45)
if number in myNumberList:
continue
else:
myNumberList.append(number)
if count == 6 - inCount:
break
else:
count = count + 1
# 2-3. 수동 모드 - 6개 번호 입력 받기
elif modeNumber == 3:
count = 1 # 입력받은 번호 갯수
while True:
number = input("6개 숫자 중 {}번째 숫자 : ".format(count))
quit(number)
# 숫자가 입력 되었는지 확인
if not number.isdigit():
print("1 ~ 45 사이 숫자를 입력하세요.")
continue
number = int(number)
# 숫자가 입력 되었으면, 1 ~ 45 사이 숫자가 맞는지 확인
if number < 1 or number > 45:
print("1 ~ 45 사이 숫자를 입력하세요.")
continue
# 1~45 사이 숫자이지만 이전에 받은 번호와 중복되진 않는지 확인
if number in myNumberList:
print("번호가 중복되었습니다. 다시 입력해 주세요.")
continue
myNumberList.append(number)
if count == 6: # 입력하려던 번호 갯수를 채우면 입력 종료
break
else:
count = count + 1
else:
print("모드 선택이 잘못되었습니다.")
# 번호 6개 모두 제대로 다 저장되었으면 오름차순 정렬
myNumberList.sort()
# print(myNumberList)
return myNumberList
# 3. 로또 번호 추첨
def lottoNumber():
lottoNumberList = random.sample(range(1, 46), 6) # 번호 6개 추첨
lottoNumberList.sort()
# 보너스 번호 추첨
while True:
bonus = random.randint(1, 45)
if bonus in lottoNumberList:
continue
else:
lottoNumberList.append(bonus)
break
print("$" * 50)
print("이번주 로또 번호는??")
for i in lottoNumberList[:6]:
print(i, end=" ")
print("+", bonus)
print("$" * 50)
return lottoNumberList
def resultPrint(myNumberList, lottoNumberList):
print("내 로또 번호 : ", end="")
# 맞는 번호에 동그라미(괄호) 쳐서 출력해주면서 맞는 갯수 카운트(보너스 번호는 (()) 로 감싸기)
for i in myNumberList:
if i in lottoNumberList[:6]:
print("({})".format(i), end=" ")
elif i == lottoNumberList[6]:
print("(({}))".format(i), end=" ")
else:
print(i, end=" ")
print("")
print("$" * 50)
# 4. 당첨 결과 판정하여 등수 리턴
def result(myNumberList, lottoNumberList):
count = 0 # 맞는 번호 갯수
# 맞는 번호에 동그라미(괄호) 쳐서 출력해주면서 맞는 갯수 카운트(보너스 번호는 (()) 로 감싸기)
for i in myNumberList:
if i in lottoNumberList[:6]:
count = count + 1
# 맞는 번호 갯수에 따른 등수 판정
rank = 0
if count == 6:
rank = 1
elif count == 5:
if lottoNumberList[6] in myNumberList:
rank = 2
else:
rank = 3
elif count == 4:
rank = 4
elif count == 3:
rank = 5
elif count <= 2 or count >= 0:
rank = 6
else:
print("error 발생")
exit()
return rank
# (1)~(4) 추가
# (1) 로또를 산 랜덤 수의 사람 수 저장(4000만~5000만)
def peopleNumber():
people = random.randint(40000000, 50000000)
return people
# (2) 총 판매액 = 1,000원 * people
# 총 당첨금 = 총 판매액 * 50%
def totalPrize(people):
totalSell = 1000 * people
totalPrizeMoney = totalSell * 0.5
return totalPrizeMoney
# (3) 랜덤 수의 사람들의 로또 당첨 판정하여 등수 별로 몇 명 당첨되었는지 저장
def rankPeople(people, lotto):
rankPeopleNum = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
# 사람 수만큼 로또 번호 6개씩 뽑아서 매번 당첨 여부 확인(people-1 한 이유: 내 번호는 따로 뽑으니까)
for i in range(people-1):
personLotto = random.sample(range(1, 46), 6)
rank = result(personLotto, lotto)
# print(i+1)
if rank != 6:
rankPeopleNum[rank] += 1
return rankPeopleNum
# (4) 등수별 당첨자 수에 따른 등수별 당첨 금액 지정
# - 1등 : 총 당첨금 중 4,5등 총 당첨금을 제외한 75%
# - 2등 : 총 당첨금 중 4,5등 총 당첨금을 제외한 12.5%
# - 3등 : 총 당첨금 중 4,5등 총 당첨금을 제외한 12.5%
# - 4등 : 50,000원
# - 5등 : 5,000원
def rankPrize(money, rankPeopleNum):
rankmoney = {1: 0, 2: 0, 3: 0, 4: 50000, 5: 5000}
exclude45 = money - rankmoney[5] * rankPeopleNum[5] - rankmoney[4] * rankPeopleNum[4]
if rankPeopleNum[3] != 0:
rankmoney[3] = round(exclude45 * 0.125 / rankPeopleNum[3])
else:
rankmoney[3] = round(exclude45 * 0.125)
if rankPeopleNum[2] != 0:
rankmoney[2] = round(exclude45 * 0.125 / rankPeopleNum[2])
else:
rankmoney[2] = round(exclude45 * 0.125)
if rankPeopleNum[1] != 0:
rankmoney[1] = round(exclude45 * 0.75 / rankPeopleNum[1])
else:
rankmoney[1] = round(exclude45 * 0.125)
return rankmoney
# 5. 등수에 따른 당첨금 출력
def prize(rank, rankmoney):
if rank <= 5:
print("{}등 당첨을 축하드립니다!!!".format(rank))
else:
print("아쉽게도 낙첨 되셨습니다 ㅠㅠ")
if rank == 1:
print("당첨금은 {:,} 원 입니다.".format(rankmoney[1]))
elif rank == 2:
print("당첨금은 {:,} 원 입니다.".format(rankmoney[2]))
elif rank == 3:
print("당첨금은 {:,} 원 입니다.".format(rankmoney[3]))
elif rank == 4:
print("당첨금은 {:,} 원 입니다.".format(rankmoney[4]))
elif rank == 5:
print("당첨금은 {:,} 원 입니다.".format(rankmoney[5]))
elif rank == 6:
print("다음 기회에...")
else:
print("rank값 error")
exit()
while True:
# 모드 선택
modeNumber = modeSelect()
# 모드에 따른 내 로또 번호 저장
myNumberList = myNumber(modeNumber)
# 로또 번호 추첨
lottoNumberList = lottoNumber()
# 사람 수 랜덤 생성
people = 1000000 #peopleNumber() 너무 오래 걸려서 임시로 100만 명으로 입력해 놓음
# 사람 수에 따른 총 당첨금 계산
total = totalPrize(people)
# 등수 별 당첨자 수 계산
rankcount = rankPeople(people, lottoNumberList)
rank = result(myNumberList, lottoNumberList)
if rank != 6:
rankcount[rank] += 1
print("등수별 당첨자 수:", rankcount)
# 등수 별 당첨자 수에 따른 등수 별 당첨금 계산
rankmoney = rankPrize(total, rankcount)
print("등수별 당첨금:", rankmoney)
# 내 번호의 등수와 당첨금 출력
resultPrint(myNumberList, lottoNumberList)
prize(rank, rankmoney)
quit(input("계속하려면 Enter, 종료하려면 'q'를 입력하세요 : "))
1) 저번에 추가하려던 내용 추가
1. 로또를 산 랜덤 수의 사람 수 저장(4000만~5000만) => people
2. 총 판매액 = 1,000원 * people
3. 총 당첨금 = 총 판매액 * 50%
4. 랜덤 수의 사람들의 로또 당첨 판정하여 등수 별로 몇 명 당첨되었는지 저장
5. 그에 따른 등수별 당첨 금액 지정
- 1등 : 총 당첨금 중 4,5등 총 당첨금을 제외한 75%
- 2등 : 총 당첨금 중 4,5등 총 당첨금을 제외한 12.5%
- 3등 : 총 당첨금 중 4,5등 총 당첨금을 제외한 12.5%
- 4등 : 50,000원
- 5등 : 5,000원
6. 내 로또 번호는 몇 등에 얼마 받을지 출력
2) 사람 수 랜덤으로 4000만~5000만 들어가게 했으나, 뽑는 데 너무 오래걸려서 일단 100만으로 입력해 놓음
3) 100만으로 하니까 1등 안나옴.. 2등은 어쩌다 한 번 나오는 듯..
728x90
'Programmer's Diary > Python' 카테고리의 다른 글
[Python] 2022-02-15 개인 Project _ DB연습2_농구스코어DB(class 및 상속 활용) (0) | 2022.02.15 |
---|---|
[Python] 2022-02-10 개인 Project _ DB연습_농구스코어DB (0) | 2022.02.12 |
[Python] 2022-02-07 개인 Project _ 로또 시뮬레이션 게임 (0) | 2022.02.12 |
[Python] 2022-02-05 개인 Project _ 재미로 만든 숫자야구게임 (0) | 2022.02.12 |
[Python] 2022-01-30 개인 Project _ 재미로 만든 로또 번호 추첨 프로그램(수정1) (0) | 2022.02.12 |