您的当前位置:首页正文

石头剪刀布小游戏(抢5胜,python代码实现)

2024-11-26 来源:个人技术集锦
# python代码如下
import random
win = 0
tie = 0
lose = 0
while win < 5 and lose < 5:
    while True:
        player = int(input("请出拳 石头(1)/剪刀(2)/布(3):"))
        if player in [1,2,3]:
            break
        else:
            print("非法输入,请重新出拳!")
            continue
    computer = random.randint(1, 3)
    print("电脑出的是:%d" % computer)
    if (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1):
        win += 1
        print("恭喜,这一局你赢了!")
    elif player == computer:
        tie += 1
        print("平局,下一局!")
    else:
        lose += 1
        print("不好意思,这一局电脑赢了。")
    print("胜%d负%d平%d"%(win,lose,tie))
    if win == 5:
        print("恭喜你,比赛获胜!")
    if lose == 5:
   print("很遗憾,比赛败北!")
显示全文