Python实现剪刀石头布
作者:互联网
Python实现剪刀石头布
结果演示如下:
数字输入正确
数字输入不正确
代码实现
# 实现剪刀石头布游戏
import random # 引入随机库
a = random.randint(0, 2) # 随机数字0,1,2
while True:
x = int(input("请输入数字0,1,2(剪刀为0,石头为1,布为2):"))
if x <= 2 and x >= 0:
print("你输入数字正确")
break
elif x > 2:
print("你输入数字有误,请重新输入")
if x == 0:
print("你输入是:剪刀")
if x == 1:
print("你输入的是:石头")
else:
print("你输入的是:布")
print("生成的随机数是:", a)
if a == 0:
print("生成的是:剪刀")
if a == 1:
print("生成的是:石头")
else:
print("生成的是:布")
if a == x:
print("平局")
elif x == 0:
if a == 1:
print("哈哈,你输了")
elif a == 2:
print("哈哈,你赢了")
elif x == 1:
if a == 0:
print("哈哈,你输了")
elif a == 2:
print("哈哈,你赢了")
elif x == 2:
if a == 0:
print("哈哈,你输了")
elif a == 1:
print("哈哈,你赢了")
标签:elif,Python,石头,哈哈,print,剪刀,输入 来源: https://blog.csdn.net/qq_44711575/article/details/122450941