Python编写猜拳游戏

########## 猜拳游戏 ##########
import random

剪刀 = 0
石头 = 1
布 = 2
flag = True
while flag:
    a = random.randint(0,2)
    print("-"*10,"游戏开始","-"*10)
    b = input("请输入剪刀(0),石头(1),布(2):")
    while b not in ['剪刀','石头','布','0','1','2']:
        b = input("输入有误,请输入剪刀,石头或布")
    
    b = int(b)
    if b == a:
        print("你输入的是",b)
        print("随机生成数字为:",a)
        print("平局")
    elif (b == 0 and a == 2) or (b == 1 and a == 0) or (b == 2 and a ==1):
        print("你输入的是",b)
        print("随机生成数字为:",a)
        print("你赢了")
    else:
        print("你输入的是",b)
        print("随机生成数字为:",a)
        print("你输了")
    choice = input("是否继续游戏?(y/n)")
    if choice.lower() == 'n':
        flag = False
        print("-"*10,"游戏结束","-"*10)

Python编写猜拳游戏_第1张图片

你可能感兴趣的:(python)