본문 바로가기
파이썬

[파이썬] 로또 생성기

by jackypark 2022. 7. 26.

자바에서 했던 로또 생성기 파이썬으로 바꾸기

import random

lottoNum=random.sample(range(1,46),7) ## 중복없는 난수 1부터 45까지 7개 생성해서 튜플에 넣기
bonusNum=lottoNum[-1] ## 마지막번호는 보너스 번호이다
lottoNum=tuple(lottoNum[:6])

def firstPlace():
    print("---------------1등 당첨번호----------------")
    count=0
    j=0
    while j<6:
        if count==6:
            break
        print(lottoNum[j],end='\t')
        j+=1
        count+=1
    print()
def secondPlace():
    print("---------------2등 당첨번호----------------")
    for i in range(6):
        count=0
        j=0
        while j<6:
            if count==i:
                print(bonusNum,end='\t')
            else:
                print(lottoNum[j],end='\t')
            j+=1
            count+=1
        print()

def thirdPlace():
    print("---------------3등 당첨번호----------------")
    for i in range(6):
        count=0
        j=0
        while j<6:
            if count==i:
                print("X",end='\t')
            else:
                print(lottoNum[j],end='\t')
            j+=1
            count+=1
        print()

def fourthPlace():
    print("---------------4등 당첨번호----------------")
    for i in range(6):
        for j in range(i+1,6):
            count = 0
            k=0
            while k< 6:
                if count == i:
                    print("X", end='\t')
                elif count == j:
                    print("X", end='\t')
                else :
                    print(lottoNum[k], end='\t')
                count += 1
                k += 1
            print()
            k = 0

def fifthPlace():
    print("---------------5등 당첨번호----------------")
    for i in range(6):
        for j in range(i+1,6):
            for k in range(j+1,6):
                count=0
                l=0
                while l<6:
                    if count == i:
                        print("X", end='\t')
                    elif count == j:
                        print("X", end='\t')
                    elif count == k:
                        print("X", end='\t')
                    else :
                        print(lottoNum[l], end='\t')
                    count+=1
                    l+=1
                print()
                

def findPlace(): ## 내로또 등수 확인
    myLottoNum=map(int,input('내로또번호 6개 입력: ').split()) 
    myLottoNum=tuple(myLottoNum)
    cnt=0
    for i in range(6):
        val=lottoNum[i] in myLottoNum
        if val:
            cnt+=1
    
    bval=bonusNum in myLottoNum
    
    if cnt==6:
        print("1등 당첨입니다")
    elif cnt==5:
        if bval:
            print("2등 당첨입니다")
        else:
            print("3등 당첨입니다")
    elif cnt==4:
        print("4등 당첨입니다")
    elif cnt==3:
        print("5등 당첨입니다")
    else:
        print("꽝")    


while True:
    choose=int(input("1.내 등수 확인하기 2. 1등 당첨번호 3. 2등 당첨번호 4. 3등 당첨번호 5. 4등 당첨번호 6. 5등 당첨번호 0.시스템종료"))   
    
    if choose == 1:
        findPlace()
    elif choose == 2:
        firstPlace()
    elif choose == 3:
        secondPlace()
    elif choose == 4:
        thirdPlace()
    elif choose == 5:
        fourthPlace()
    elif choose == 6:
        fifthPlace()
    elif choose== 0:
        break
    else:
        print("잘못입력하셧습니다")

파이썬 객체지향언어로 알고 있는데 c언어처럼 짜버린 거 같은 건 기분 탓인걸 까요? 더 공부 많이해서 객체 지향적으로 짜보도록 노력해야겠다

댓글