简单猜拳游戏
分析:
1.玩家输入拳法:石头1 剪刀2 布3 用数字代替拳法
2.电脑随机产生一个拳法 采用随机函数
3.判断拳法:a 玩家获胜
b 平局
c 玩家失败
代码:
import random
#玩家输入拳法:石头1 剪刀2 布3
# str转化为int方便判断
player = int(input('请输入的你的拳法(石头1 剪刀2 布3):'))
#电脑利用随机函数产生拳法,
#randint(),两边都是闭区间
pc = random.randint(1,3)
print('玩家:%d《---》电脑:%d' %(player,pc))
#判断输出用if elif else
if (player==1 and pc == 2) or (player == 2 and pc == 3) or (player == 3 and pc == 1):
print('玩家获胜')
elif player == pc:
print('平局')
else:
print('玩家失败')
运行结果:
D:\PyCharmProject\venv\Scripts\python.exe D:/PyCharmProject/Test2/简单猜拳游戏.py
请输入的你的拳法(石头1 剪刀2 布3):3
玩家:3《---》电脑:2
玩家失败
Process finished with exit code 0