python中 if elif else的使用
"""猜拳游戏"""
import random # 电脑随机模块
player = int (input("请输入:剪刀(0) 石头(1) 布(2)"))
compute = random.randint(0,2)
# 玩家胜利
if (player == 0 and compute == 2) or (player == 1 and compute ==0) or (player ==2 and compute ==1):
print("玩家胜利,欧耶!")
elif player ==compute:
print("平局")
else:
# 电脑胜利
print("电脑胜利,哈哈哈")
加入 while True可以循环进行猜拳游戏
在print("来了老弟 来玩王者猜拳游戏")
while True:
import random # 电脑随机模块
player = int (input("请输入:剪刀(0) 石头(1) 布(2)"))
compute = random.randint(0,2)
# 玩家胜利
if (player == 0 and compute == 2) or (player == 1 and compute ==0) or (player ==2 and compute ==1):
print("玩家胜利,欧耶!")
elif player ==compute:
print("平局")
else:
# 电脑胜利
print("电脑胜利,哈哈哈")