python简单猜拳游戏循环

import random

# 出拳游戏
dict1 = {0: "石头", 1: "剪刀", 2: "布", 3: "退出"}  # 建立一个存储库
i = 1  # 猜拳次数
num_player = 0  # 玩家赢得局
num_computer = 0  # 电脑赢得局
num_pc = 0  # 平局
num_js = int(input("请输入要玩的局数:"))
while i <= num_js:
    player = int(input("猜拳游戏:石头【0】 剪刀【1】 布【2】 退出【3】"))
    while player not in dict1.keys():  # 判断用户输入的值是否在存储库内
        player = int(input("输入有误,请输入您的选项:"))

    if player == 3:
        print("游戏结束!")
        print("猜拳总%d局;玩家赢%d局,电脑赢%d局,平%d局!欢迎下次挑战!" % (i - 1, num_player, num_computer, num_pc))
        break
    computer = random.randint(0, 2)  # 随机取0-2之间的数

    print("电脑出的是:%s" % dict1.get(computer))
    print("你出的是:%s" % dict1.get(player))

    # 玩家赢:石头对剪刀,即玩家为0,电脑为1;剪刀对布,即玩家为1,电脑为2;布对石头,即玩家为2,电脑为0;
    if (player == 0 and computer == 1) or (player == 1 and computer == 2) or (player == 2 and computer == 0):
        print("你赢了!猜拳次数为:%d" % i)
        i += 1
        num_player += 1

    # 平局
    elif player == computer:
        print("平局!猜拳次数为:%d" % i)
        i += 1
        num_pc += 1

    # 你输了
    else:
        print("你输了!猜拳次数为:%d" % i)
        i += 1
        num_computer += 1
else:
    print("游戏结束!")
    print("猜拳总%d局;玩家赢%d局,电脑赢%d局,平%d局!欢迎下次挑战!" % (i - 1, num_player, num_computer, num_pc))
    if num_player > num_computer:
        print("你真棒!")
    elif num_player == num_computer:
        print("你居然连npc都赢不了!")
    else:
        print("你可真菜!")

 

你可能感兴趣的:(python,游戏,开发语言)