Python猜拳游戏


import random

print("您开启猜拳游戏....")

wins_count = 0 #赢得次数
fail_count = 0 #输的次数
draw_count = 0 #平局的次数
computer = "" #电脑的出拳
n = 1 #游戏局数
k = 0;#猜拳次数

while True:

    print("\n第{}局".format(n)) #打印局数
    n+=1
    player = input("请输入您出什么(石头 or 剪刀 or 布): ") #玩家输入
    index = random.randint(0,2) #随机生成0-2数字

    #把随机电脑随机生成的数字转为字符串
    if index == 0 :
        computer = "石头"
    elif index == 1:
        computer = "剪刀"
    elif index == 2:
        computer = "布"

    #判断
    if player == computer:
        print("玩家出的{} \n电脑出的{}".format(player,computer))
        print("平局")
        draw_count += 1
        k+=1
    elif (player == "布" and computer == "石头") or (player == "石头" and computer == "剪刀") or (player == "剪刀" and computer == "布"):
        print("玩家出的{} \n电脑出的{}".format(player,computer))
        print("您赢了!!!")
        wins_count += 1
        k+=1
    elif (player == "布" and computer == "剪刀") or (player == "石头" and computer == "布") or (player == "剪刀" and computer == "石头"):
        print("玩家出的{} \n电脑出的{}".format(player,computer))
        print("您输了...")
        fail_count += 1
        k+=1
    else:
        print("您的输入有误!!!请重新输入...")
        n-=1
        continue #跳过此次循环

    if n == 4: #玩到三局游戏进入判断
        key = int(input("三局游戏已到....请问要继续游戏吗?(1:不了 2:继续): "))
        if key == 1:
            print("游戏结束...开始结算....您共进行了{}次游戏".format(k))
            print("您赢了{}次 , 输了{} 次, 平局{}次".format(wins_count,fail_count,draw_count)) #打印结果
            #重置信息
            wins_count = 0 #赢得次数
            fail_count = 0 #输的次数
            draw_count = 0 #平局的次数
            computer = "" #电脑的出拳
            n = 1 #游戏局数
            break
        elif key == 2:
            print("游戏继续...")
            n = 1
            continue




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CuteTTU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值