08.개발&프로그래밍/1.파이썬
4. Python Rock–Paper–Scissors (가위바위보) 게임
JWJ Family
2025. 7. 18. 23:18
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()
반응형