剪刀石头布(三局两胜)

时间:2021-08-31 16:49:22

(1)通过最简单的选择和循环实现(但代码过长) import random computer_win_count = 0
people_win_count = 0 while True:
    computer = random.randint(0,2)
    people = int(input("请做出选择(0:石头 1:剪刀 2:布):"))
    if computer == people == 0:
        print("电脑:石头   玩家:石头   结果:平局")
    elif computer == people == 1:
        print("电脑:剪刀   玩家:剪刀   结果:平局")
    elif computer == people == 2:
        print("电脑:布   玩家:布   结果:平局")
    elif computer == 0 and people == 1:
        print("电脑:石头  玩家:剪刀  结果:你输了")
        computer_win_count = 1
    elif computer == 0 and people == 2:
        print("电脑:石头  玩家:布  结果:你赢了")
        people_win_count = 1
    elif computer == 1 and people == 0:
        print("电脑:剪刀  玩家:石头  结果:你赢了")
        people_win_count = 1
    elif computer == 1 and people == 2:
        print("电脑:剪刀  玩家:布  结果:你输了")
        computer_win_count = 1
    elif computer == 2 and people == 0:
        print("电脑:布  玩家:石头  结果:你输了")
        computer_win_count = 1
    elif computer == 2 and people == 1:
        print("电脑:布  玩家:剪刀  结果:你赢了")
        people_win_count = 1
   
    if computer_win_count == 2 and people_win_count < 2:
        print("最终结果:电脑获胜")
        break
    if computer_win_count < 2 and people_win_count == 2:
        print("最终结果:你获胜")
        break