Python

(파이썬) 가위바위보 게임

고니자니 2023. 5. 24. 20:51
반응형

파이썬으로 만든 가위바위보 게임입니다.

사용자가 가위, 바위, 보 중에서 하나를 입력하면 컴퓨터가 임의로 가위, 바위, 보 중에서 한 개를 결정해서 누가 이겼는지 결정합니다.

 

"게임을 다시 하시겠습니까? (y/n):"를 묻고, 게임을 다시 할 지 또는 종료할지를 결정합니다.

import random

def get_user_choice():
    while True:
        user_choice = input("가위, 바위, 보 중 하나를 선택하세요: ")
        if user_choice in ['가위', '바위', '보']:
            return user_choice
        else:
            print("잘못된 입력입니다. 다시 입력해주세요.")

def get_computer_choice():
    choices = ['가위', '바위', '보']
    computer_choice = random.choice(choices)
    return computer_choice

def determine_winner(user_choice, computer_choice):
    print("사용자: " + user_choice)
    print("컴퓨터: " + computer_choice)
    if user_choice == computer_choice:
        print("비겼습니다!")
    elif (
        (user_choice == '가위' and computer_choice == '보') or
        (user_choice == '바위' and computer_choice == '가위') or
        (user_choice == '보' and computer_choice == '바위')
    ):
        print("사용자가 이겼습니다!")
    else:
        print("컴퓨터가 이겼습니다!")

def play_again():
    while True:
        again = input("게임을 다시 하시겠습니까? (예/아니오): ")
        if again == '예':
            return True
        elif again == '아니오':
            return False
        else:
            print("잘못된 입력입니다. 다시 입력해주세요.")

def play_game():
    while True:
        user_choice = get_user_choice()
        computer_choice = get_computer_choice()
        determine_winner(user_choice, computer_choice)
        if not play_again():
            break

print("가위바위보 게임을 시작합니다.")
play_game()

728x90
반응형