Python小游戏——石头剪刀布
python 知识点
random 随机函数包调用 randint() 函数生成随机数。
利用 if…elif…else 分支结构进行胜负判断。
游戏规则
为简化流程,利用数字代替石头/剪刀/布
出拳规则
1–代表石头
2–代表剪刀
3–代表出布
代码块
import random
#玩家出拳
player=int(input("请输入您要出的拳: 石头(1)/剪刀(2)/布(3):"))
#电脑利用随机函数出拳
computer=random.randint(1,3)
print("玩家出拳为%d-电脑玩家出拳为%d"%(player,computer))
if (player==1 and computer==2)or (player==2 and computer==3)or (player==3 and computer==1):
print("玩家胜利!!!")
elif player==computer:
print("平局")
elif player>3:
print("请按规则出拳。")
else:
print("电脑胜利!!!")