python 小案例demo05 升级版石头剪刀布


#1、 猜拳 (用if elif else)

# 导入随机数
import random
# 判断是否为 0~2这个范围的 并且返回chinses 和 num
def nums(num):
chinese = ""
while 1:
if 0 <= int(num) <= 2:
if int(num) == 0:
chinese = "石头"
elif int(num) == 1:
chinese = "剪刀"
elif int(num) == 2:
chinese = "布"
break
else:
num = input("输入错误,请重新输入")
continue


return chinese,int(num)



#用户
user = input("请猜拳(0为石头 1为剪刀 2为布)")
#获取到用户输入的结果
_uchinese,_unum = nums(user)
print("你:%s"%_uchinese)
#电脑
num = random.randint(0,2)
#获取到电脑随机得到的结果
_cchinese,_cnum = nums(num)
print("电脑:%s"%_cchinese)

#判断输赢
if _unum == _cnum:
print("平局了")
elif _unum == 0:
if _cnum == 1:
print("恭喜您赢了!")
else:
print("很遗憾您输了!")
elif _unum == 1:
if _cnum == 0:
print("很遗憾您输了!")
else:
print("恭喜您赢了")
elif -_unum == 2:
if _cnum == 0:
print("恭喜您赢了!")
else:
print("很遗憾您输了!")
posted @ 2020-10-27 21:38  AxeBurner  阅读(117)  评论(0编辑  收藏  举报