Computer Programming/AI

TIL_두 번째 assignment(가위바위보 Game)

JYCoder 2023. 8. 22. 15:22

Python을 사용하여 '가위바위보' 게임을 만들었다.

 

여기서 사용한 기능은 다음과 같다.

random, while-break, try-except, 다중 if, dictionary, .upper(), input()

 

import random


result = {'win': 0, 'lose': 0, 'tie': 0}

while True:
    try:
        input_choice = input('(가위/바위/보) 중 하나를 선택하세요: ')
        if input_choice != '가위' and input_choice != '바위' and input_choice != '보':
            raise ValueError

        option_list = ['가위', '바위', '보']
        random_choice = random.choice(option_list)

        if (input_choice == '가위' and random_choice == '보') or (input_choice == '바위' and random_choice == '가위') or (input_choice == '보' and random_choice == '바위'):
            print('Me: ', input_choice)
            print('Computer: ', random_choice)
            print('WIN!')
            result['win'] += 1

        if (input_choice == '가위' and random_choice == '바위') or (input_choice == '바위' and random_choice == '보') or (input_choice == '보' and random_choice == '가위'):
            print('Me: ', input_choice)
            print('Computer: ', random_choice)
            print('Lose..')
            result['lose'] += 1

        if input_choice == random_choice:
            print('Me: ', input_choice)
            print('Computer: ', random_choice)
            print('Tie')
            result['tie'] += 1

        answer = input('게임을 다시 하겠습니까(yes/no)?').upper()

        if answer == 'NO':
            print(result)
            print('Bye!')
            break
    except ValueError:
        print("Oops!!! '가위/바위/보' 중 입력해 주세요.")

어제 'up-and-down' game을 한 번 만들어 봤더니 오늘 두 번째 게임을 만드는 것이 매우 수월했다. 아직은 text로만 되어있고 간단한 게임이지만 내가 작성한 게임이라서 그런지 좀 재밌다. test를 하는 과정에서 다양한 case가 있을 것을 예상하고 적절하게 대처하는 코드를 생각하다 보니 이렇게 간단한 게임에도 엄청난 정성이 들어가야 한다는 것을 알았다. 내가 작성한 게임이라 그런지 코드를 볼 때마다 애틋하고 뿌듯하다.

 

이제 class 사용법에 대해 연습해야겠다.

LIST

'Computer Programming > AI' 카테고리의 다른 글

TIL_Algorithm  (0) 2023.08.24
TIL_Assignment 3(class를 이용한 회원/게시글 관리)  (0) 2023.08.23
TIL_Python Assignment 1(Up-and-Down Game)  (0) 2023.08.21
WIL_두 번째 주  (0) 2023.08.20
TIL_Python 문법 심화  (0) 2023.08.18