728x90
아래 코드는 터미널에서 실행 가능한 간단한 가위바위보 게임입니다. 컴퓨터와 번갈아 가위, 바위, 보를 선택하여 승패를 확인할 수 있습니다. 코드를 rps.py
로 저장한 뒤, python rps.py
로 실행해 보세요.
# rps.py
import random
def get_user_choice():
choices = {'가위': '가위', '바위': '바위', '보': '보'}
while True:
user = input("가위, 바위, 보 중 하나를 선택하세요: ")
if user in choices:
return user
print("잘못된 입력입니다. '가위', '바위', '보' 중 하나를 입력해주세요.")
def get_computer_choice():
return random.choice(['가위', '바위', '보'])
def determine_winner(user, comp):
if user == comp:
return "무승부"
wins = {
'가위': '보',
'바위': '가위',
'보': '바위'
}
if wins[user] == comp:
return "사용자 승리"
else:
return "컴퓨터 승리"
def main():
print("=== 가위바위보 게임 ===")
while True:
user_choice = get_user_choice()
comp_choice = get_computer_choice()
print(f"사용자: {user_choice} 컴퓨터: {comp_choice}")
result = determine_winner(user_choice, comp_choice)
print(f"결과: {result}\n")
again = input("다시 플레이하시겠습니까? (y/n): ").lower()
if again != 'y':
print("게임을 종료합니다. 감사합니다!")
break
if __name__ == "__main__":
main()
반응형
'08.개발&프로그래밍 > 1.파이썬' 카테고리의 다른 글
5. Python Number Guessing Game (숫자 맞추기 게임) (0) | 2025.07.18 |
---|---|
3. Python Pong Game (퐁 게임) (0) | 2025.07.18 |
2. Python Snake Game (뱀 게임) (0) | 2025.07.18 |
1. Python으로 구현한 틱택토 (Tic-Tac-Toe) 게임 (0) | 2025.07.18 |
15. Python Logical Errors(로직 오류) 완벽 가이드 (0) | 2025.07.18 |