python实现猜拳游戏,统计玩的次数、赢的次数、胜率
import random
# 用于识别是否继续游戏
Game = True
# 游戏次数
GameCount = 0
# 赢的次数
WinCount = 0
while Game:
# 利用随机函数,生成0-2的数,用于系统出拳
sQuan = random.randint(0, 2)
# 用户出拳
uQuan = input("请出拳(0石头,1剪刀,2布,q退出):")
try:
# 把用户的输入的值,转为int
uQuan = int(uQuan)
# 游戏数加1
GameCount += 1
# 平局
if (sQuan == 0 and uQuan == 0) or (sQuan == 1 and uQuan == 1) or (sQuan == 2 and uQuan == 2):
print("平均,系统是{},您是{}。".format(sQuan, uQuan))
# 赢的三种情况: 系统1剪刀,用户0石头;系统2布,用户1剪刀;系统0石头,用户2布
elif (sQuan == 1 and uQuan == 0) or (sQuan == 2 and uQuan == 1) or (sQuan == 0 and uQuan == 2):
print("您赢了,系统是{},您是{}。".format(sQuan, uQuan))
# 赢的次数加1
WinCount += 1
# 除了平局、赢,其他都是输
else:
print("您输了,系统是{},您是{}。".format(sQuan, uQuan))
except (IOError, ZeroDivisionError, Exception) as e:
print("退出游戏,你玩了{}次,赢了{}次,胜率{}%".format(GameCount, WinCount, round((WinCount/GameCount)*100, 0)))
# 退出while循环
Game = False