Python
(파이썬) 가위바위보 게임
코딩ABC
2023. 11. 14. 11:28
반응형
사람과 컴퓨터가 가위바위보 게임을 하는 파이썬 코드입니다.
import random
def get_user_choice():
user_choice = input("가위, 바위, 보 중 하나를 입력하세요: ").lower()
while user_choice not in ['가위', '바위', '보']:
print("잘못된 입력입니다. 가위, 바위, 보 중 하나를 다시 입력하세요.")
user_choice = input("가위, 바위, 보 중 하나를 입력하세요: ").lower()
return user_choice
def get_computer_choice():
return random.choice(['가위', '바위', '보'])
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 "컴퓨터가 이겼습니다!"
print("가위바위보 게임을 시작합니다!")
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print("사람:", user_choice)
print("컴퓨터:", computer_choice)
result = determine_winner(user_choice, computer_choice)
print(result)
반응형