더북(TheBook)

게임이 완성되었습니다. 전체 코드를 살펴보겠습니다.

코드 부록-8 appendix/number_guess_game2.py ⑤

import random

result = [None, None, None]

for i in range(3):
    rand_num = random.randint(1, 9)
    # print("com : ", rand_num)

    player_num = input("Make a guess(1~9) : ")
    while not player_num.isdigit():
        print("Must be an integer from 1 to 9")
        player_num = input("Make a guess(1~9) : ")

    player_num = int(player_num)

    if rand_num = = player_num:
        result[i] = "player"
    else:
        result[i] = "computer"

# print(result)

print("player : {}, computer : {}".format(
    result.count("player"),
    result.count("computer")))

if result.count("player") >= 2:
    print("You win!")
else:
    print("You lose!")

실행결과 Make a guess(1~9) : a
Must be an integer from 1 to 9
Make a guess(1~9) : 가
Must be an integer from 1 to 9
Make a guess(1~9) : 3
Make a guess(1~9) : 6
Make a guess(1~9) : 2
player : 1, computer : 2
You lose!

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.