상세 컨텐츠

본문 제목

가위 바위 보 게임 파이썬 코드

시사경제지식

by 브로마리 2023. 8. 25. 02:00

본문

반응형

import random

def get_user_choice():
    choice = input("가위, 바위, 보 중 하나를 선택하세요: ").strip().lower()
    if choice not in ["가위", "바위", "보"]:
        print("올바른 선택이 아닙니다. 다시 시도하세요.")
        return get_user_choice()
    return choice

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

def determine_winner(user_choice, computer_choice):
    if user_choice == computer_choice:
        return "무승부"
    elif (
        (user_choice == "가위" and computer_choice == "보") or
        (user_choice == "바위" and computer_choice == "가위") or
        (user_choice == "보" and computer_choice == "바위")
    ):
        return "사용자 승리"
    else:
        return "컴퓨터 승리"

def main():
    print("가위 바위 보 게임을 시작합니다.")
    while True:
        user_choice = get_user_choice()
        computer_choice = get_computer_choice()
        print(f"사용자: {user_choice}, 컴퓨터: {computer_choice}")
        result = determine_winner(user_choice, computer_choice)
        print(f"결과: {result}")
        
        play_again = input("다시 하시겠습니까? (y/n): ").strip().lower()
        if play_again != "y":
            print("게임 종료")
            break

if __name__ == "__main__":
    main()

반응형

궁금할 만한 핫토픽 모음

아이폰16 Pro, 자급제, 사전 예약, 네이버 쇼핑 top 50




김포 재개발 대장주 한강 수자인 오브센트




챗GPT GPT-4o 무료로 그림 그리기



관련글 더보기