版本一:
from random import random
while True:
guess=int(input("请输入你猜的内容 [2:剪刀、布:1、0:石头]:"))
if(guess != 2 and guess != 1 and guess != 0 ) :
print("数值不符合规定,请从新输入您的猜的内容!")
break
if guess ==1:
print("您出布")
elif guess ==2:
print("您出剪刀")
else :
print("您出石头")
guess_computer=int(2*random())
if guess_computer ==1:
print("电脑出布")
elif guess_computer ==2:
print("电脑出剪刀")
else :
print("电脑出石头")
if(guess == 2 and guess_computer == 2) or (guess == 1 and guess_computer == 1) or (guess == 0 and guess_computer == 0) :
print("平局")
elif (guess == 2 and guess_computer == 1) or (guess == 1 and guess_computer == 0) or (guess == 0 and guess_computer == 2) :
print("您赢了!!!")
else:
print("你输了,Don't Worried!")
版本2:在版本1的基础上想实现 布5剪2石0 利用random.choice()实现
import random
while True:
guess=int(input("请输入你猜的内容 [布:5、剪刀:2、石头:0]:"))
if(guess != 5 and guess != 2 and guess != 0 ) :
print("数值不符合规定,请从新输入您的猜的内容!")
break
if guess ==5:
print("您出布")
elif guess ==2:
print("您出剪刀")
else :
print("您出石头")
a=(5,2,0)
guess_computer = random.choice(a)
if guess_computer ==5:
print("电脑出布")
elif guess_computer ==2:
print("电脑出剪刀")
else :
print("电脑出石头")
if(guess == 5 and guess_computer == 5) or (guess == 2 and guess_computer == 2) or (guess == 0 and guess_computer == 0) :
print("平局")
elif (guess == 5 and guess_computer == 0) or (guess == 2 and guess_computer == 5) or(guess == 0 and guess_computer == 2) :
print("您赢了!!!")
else:
print("你输了。Don't Worried! Play again !!!")
版本3:希望在版本2的基础上,通过输入中文去出拳
import random
while True:
guess=input("请输入您出拳[布|剪刀|石头]:")
if(guess != '布' and guess != '剪刀' and guess != '石头' ) :
print("数值不符合规定,请从新输入您的猜的内容!")
break
print(f'您出的是{guess}')
alist=('布','剪刀','石头')
guess_computer = random.choice(alist)
print(f'电脑出的是{guess_computer}')
if(guess == guess_computer) :
print("平局")
elif (guess == '布' and guess_computer == '石头') or (guess == '剪刀' and guess_computer == '布') or(guess == '石头' and guess_computer == '剪刀') :
print("您赢了!!!")
else:
print("你输了。Don't Worried! Play again !!!")
"""
总结:
使用random必须引包import random
random() 产生[0,1)的随机数
random.randint(开始,结束) 产生自定义范围的随机数且为整型数
random.choice(seq) 产生自定义序列中的随机数,且序列中的数据类型保持不变
"""
|