본문 바로가기
알고리즘

2023-04-28 가위바위보 3판2승

by jung1911 2023. 4. 30.

 

문제 

 

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 = ['가위', '바위', '보']
computer_choice = random.choice(options)

while True:
    user_input = input("가위, 바위, 보 중에서 골라주세요: ")
    if user_input in options:
        break
    else:
        print("잘못 입력하셨습니다.")


result = match_result(user_input, computer_choice)
print(result)
 
  •  

시도 

 

user , computer의 승리 횟수를 카운트 해주고 이중구문으로 if을 사용하면 된다.

 

 

while True:
    user_input = input("가위, 바위, 보 중에서 골라주세요: ")
    if user_input in options:
        computer_choice = random.choice(options)
        battlescore = match_result(user_input, computer_choice)
        
        if battlescore == "이겼다.":
            user+=1
            print(f"유저는 {user}:컴퓨터는 {computer}")

        elif user == 2:
            print(f"유저 승리!!")
            break
        
        
        elif battlescore == "졌다.":
            computer+=1
            print(f"유저는 {user}:컴퓨터는 {computer}")
            
            
        elif computer == 2:
            print(f"computer의 승리!!")
            break
            
        
        else:
                print("비겼습니다 다시 잘 내보세요.")
                continue

            
    else:
        print("그렇게 치면 안된다구요!! 아시겠어요?!!")
        

match_result(user_input, computer_choice)

 

 

 

문제점 및 해결

 

elif로 조건을 충족 시키고 끝낼려고 했지만 어쩐지 카운트가 한 박자 느리다? 도무지 이해를 못했는데 if문을 알아보니 조건이 참이 되면 만족되었으니 elif로 넘어가는게 아니고 if 에서 끝을 내는거다.

 

 

 

 

#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 = ['가위', '바위', '보']
computer_choice = random.choice(options)
user = 0
computer = 0


while True:
    user_input = input("가위, 바위, 보 중에서 골라주세요: ")
    if user_input in options:
        computer_choice = random.choice(options)
        battlescore = match_result(user_input, computer_choice)
        
        if battlescore == "이겼다.":
            user+=1
            print(f"유저는 {user}:컴퓨터는 {computer}")

            if user == 2:
                print(f"유저 승리!!")
                break
        
        
        elif battlescore == "졌다.":
            computer+=1
            print(f"유저는 {user}:컴퓨터는 {computer}")
            
            
            if computer == 2:
                print(f"computer의 승리!!")
                break
            
        
        else:
                print("비겼습니다 다시 잘 내보세요.")
                continue

            
    else:
        print("그렇게 치면 안된다구요!! 아시겠어요?!!")
        

match_result(user_input, computer_choice)

 

 

배운점

if 가 참이 되면 elif의 조건까지 넘어가지는 않는다. 기초에서 배웠는데 돌아서니 까먹는거 같다.

댓글