# python做猜拳小游戏

``````#开发时间：2021/12/7 19:38

# 玩家通过输入012识别出拳
# 电脑随机出拳

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!")

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)
#在版本一的基础上修改这个方法
#创建一个数组去存储想要随机的数值，然后再用random.choice()这个方法去随机抽取其中的值。
#抽取出来的值不会改变数据类型
#例子证明：
#array=(1,1.1,(1,2),'a')
#ran=random.choice(array)
#print(ran,type(ran))
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 ！！！")

import random

while True:
guess=input("请输入您出拳[布|剪刀|石头]:")
if(guess != '布' and guess != '剪刀' and guess != '石头' ) :
print("数值不符合规定，请从新输入您的猜的内容！")
break
print(f'您出的是{guess}')
alist=('布','剪刀','石头')
guess_computer = random.choice(alist)
#因为random.choice(seq) 不会改变元素的数据类型的特点，简化代码。
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()                   产生[0,1)的随机数
random.randint(开始，结束)  产生自定义范围的随机数且为整型数
random.choice(seq)         产生自定义序列中的随机数，且序列中的数据类型保持不变
"""

``````

THE END

)">