정예반에서 만들어본 가위바위보 게임을 함수를 사용해서 3판2선승 게임으로 만들어봤따
import random
def match_result(user_input, computer_choice):
print(f"당신은 {user_input}을(를) 냈습니다!")
print(f"컴퓨터는 {computer_choice}을(를) 냈습니다!")
if user_input == "가위":
if computer_choice == "가위":
return "비겼다."
elif computer_choice == "바위":
return "졌다."
else:
return "이겼다."
elif user_input == "바위":
if computer_choice == "가위":
return "이겼다."
elif computer_choice == "바위":
return "비겼다."
else:
return "졌다."
else:
if computer_choice == "가위":
return "졌다."
elif computer_choice == "바위":
return "이겼다."
else:
return "비겼다."
options = ["가위", "바위", "보"]
c_count = 0
count = 0
while True:
user_input = input("가위, 바위, 보 중에서 골라주세요: ")
computer_choice = random.choice(options)
result = match_result(user_input, computer_choice)
if user_input not in options:
continue
if result == "이겼다.":
print("이겼다")
count += 1
elif result == "졌다.":
print("졋다")
c_count += 1
elif result == "비겼다.":
print("비겼다")
print(f"당신은 {count}번 이겼고 컴퓨터는 {c_count}번이겼습니다")
if count == 2:
print("당신이 이겼습니다")
break
elif c_count == 2:
print("컴퓨터가 이겼습니다")
break
count 변수를 만들어 이겼다면 count 에 +1 졌다면 c_count에 +1 해서 2번 이기는 쪽이 있다면 승패를 알려주는 문구를 출력하고 while 문을 빠져 나오게 했다