9. Python - 가위바위보 게임
2021. 2. 23. 11:48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | # 전역변수 handTable = [None, "가위", "바위", "보"] # Java -> null = Python -> None # def def printRule(): for i, h in enumerate(handTable): if i != 0: print("%d: %s" % (i, h)) # enumerate를 활용해서 (인덱스, 값) 형태의 tuple로 변환 # 인덱스와 값을 변수 i, h에 넣어준 다음 if문 # if i != 0 일때 print("%d: %s" % (i, h)) def userChoice(): uh = int(input("숫자로 적어주세요 : ")) if 1 <= uh <= 3: return uh return userChoice() # 재귀함수 # 적은 숫자가 1 ~ 3이 아닐경우 다시 입력받기 def comChoice(): return randint(1, 3) # randint(시작 값, 끝 값) : 랜덤 int def printResult(uh, ch): print("유저 : %s" % handTable[uh]) print("컴퓨터 : %s" % handTable[ch]) # 결과 값을 가위, 바위 보로 바꿔주기 # 받은 숫자 값을 만들어 놓은 handTable List에 넣어주기 def judge(uh, ch): t = uh - ch if t == 0: print("무") return 0 elif t == -1 or t == 2: print("패") return 2 else: print("승") return 1 # main # printRule() uHand, cHand, result, win, lose = 0, 0, 0, 0, 0 # while문 안에서 변수를 최초로 만드는지 # 만들어져 있는 변수에 값을 바꾸는지 # 빅데이터 돌리는 입장에서 변수를 미리 만들어 놓고 while문에서 값만 바꾸는게 좋다 while True: uHand = userChoice() cHand = comChoice() printResult(uHand, cHand) result = judge(uHand, cHand) if result == 2: lose += 1 elif result == 1: win += 1 print("%d승 %d패" % (win, lose)) | cs |
'Python' 카테고리의 다른 글
11. Python - 생성자, 소멸자 (0) | 2021.02.23 |
---|---|
10. Python - OOP (객체지향) (0) | 2021.02.23 |
8. Python - if문, for문, while문 (0) | 2021.02.23 |
7. Python - function (0) | 2021.02.23 |
6. Python - collection (0) | 2021.02.23 |