1. 程式人生 > >python猜拳遊戲

python猜拳遊戲

import random
guess = {1: "剪刀", 2: "石頭", 3: "布"}
while True:
    while True:
        player = int(input("玩家請出拳:1.剪刀 2.石頭 3.布:"))
        if player in range(1,4):
            break
        else:
            print("玩家請重新出拳:")
    computer = random.randint(1, 3)
    print("玩家出%s,電腦出%s" % (guess[player], guess[computer]))
    if (computer == 1 and player == 3) or (computer == 2 and player == 1) or (computer == 3 and player == 2):
        print("電腦贏")
    elif computer == player:
        print("平局")
    else:
        print("玩家贏")