问题描述:
提示:这里简述项目相关背景:
新学Python,写的第一个小练习
石头剪刀布:scissor(0),rock(1),paper(2):
电脑随机出,判断谁赢
使用代码以及问题
提示:这里描述项目中遇到的问题:
1.若玩家出的数不在0,1,2中,提示输入有误
2.电脑随机数用到random函数
3.import 得到的是str字符,需要用 int() 转为 数字。
coding:
提示:这里填写该问题的具体解决方案:
import random #引入随机数
x = random.randint(0,2)
user_num = int(input("请输入:scissor(0),rock(1),paper(2):"))
#为了更方便观看
if user_num not in [0,1,2]:
print("输入有误")
else:
if user_num == 0:
print("你出的是scissor")
elif user_num == 1:
print("你出的是rock")
else:
print("你出的是paper")
if x == 0:
print("电脑出的是scissor")
elif x == 1:
print("电脑出的是rock")
else :
print("电脑出的是paper")
#判断
if user_num == x:
print("打平手")
elif (user_num == 1 and x == 0) or (user_num == 2 and x == 1) or(user_num == 0 and x == 2):
print("你赢了")
else :
print("你输啦")