코딩

파이썬 random으로 만든 로또 시뮬레이터

MOJAN 2021. 5. 16. 00:14
import random
i = 1
Lotto = []
while i < 46:
    Lotto += [i]
    i += 1
random.shuffle(Lotto)
WinningNum = Lotto[0:6]
WinningNum.sort()
Bonus = Lotto[6]
print('당첨번호 : {0} {1} {2} {3} {4} {5} 보너스 번호 {6}'.format(WinningNum[0], WinningNum[1], WinningNum[2], WinningNum[3], WinningNum[4], WinningNum[5], Bonus))
print('몇 장 구매하시겠습니까?')
HowMany = int(input())
YourNum = []
Profit = HowMany * -1000
Count = 1
Result = [0, 0, 0, 0, 0, 0]
while HowMany >= 1:
    HowMany -= 1
    i = 0
    j = 0
    Correct = 0
    isBonus = 0
    random.shuffle(Lotto)
    YourNum = Lotto[0:6]
    YourNum.sort()
    while i <= 5:
        while j <= 5:
            if YourNum[i] == WinningNum[j]:
                Correct += 1
            j += 1
        i += 1
        j = 0
    i = 0
    while i <= 5:
        if YourNum[i] == Bonus:
            isBonus = 1
        i += 1
    print('{0}번째 로또 : {1} {2} {3} {4} {5} {6}'.format(Count, YourNum[0], YourNum[1], YourNum[2], YourNum[3], YourNum[4], YourNum[5]), end='\t\t')
    if Correct <= 2:
        print('낙첨되었습니다')
        Result[5] += 1
    elif Correct == 3:
        print('5등 당첨')
        Result[4] += 1
        Profit += 5000
    elif Correct == 4:
        print('4등 당첨')
        Result[3] += 1
        Profit += 50000
    elif Correct == 5:
        if isBonus == 0:
            print('3등 당첨')
            Result[2] += 1
            Profit += 1390427
        else:
            print('2등 당첨')
            Result[1] += 1
            Profit += 5422666
    elif Correct == 6:
        print('1등 당첨')
        Result[0] += 1
        Profit += 195216000
    Count += 1
print('최종 결과\n1등 : {0}회\n2등 : {1}회\n3등 : {2}회\n4등 : {3}회\n5등 : {4}회\n낙첨 : {5}회\n최종 금액 : {6}원'.format(Result[0], Result[1], Result[2], Result[3], Result[4], Result[5], Profit))

처음 짠 코드라 아주 난해하게 썼지만 굉장히 잘 작동함 제일 처음에는 당첨된 로또의 번호를 알려준다

입력 받는것은 몇장 살것인지 입력받고 출력되는것은 각 로또의 당첨 여부와 최종적으로 얼마의 손익을 봤는지 알림

이로써 알수있는건 로또로 최대 이익을 보기위해서는 적게사서 운좋게 당첨되는것 뿐이다

로또당첨되고싶다