import random
print("您开启猜拳游戏....")
wins_count = 0 #赢得次数
fail_count = 0 #输的次数
draw_count = 0 #平局的次数
computer = "" #电脑的出拳
n = 1 #游戏局数
k = 0;#猜拳次数
while True:
print("\n第{}局".format(n)) #打印局数
n+=1
player = input("请输入您出什么(石头 or 剪刀 or 布): ") #玩家输入
index = random.randint(0,2) #随机生成0-2数字
#把随机电脑随机生成的数字转为字符串
if index == 0 :
computer = "石头"
elif index == 1:
computer = "剪刀"
elif index == 2:
computer = "布"
#判断
if player == computer:
print("玩家出的{} \n电脑出的{}".format(player,computer))
print("平局")
draw_count += 1
k+=1
elif (player == "布" and computer == "石头") or (player == "石头" and computer == "剪刀") or (player == "剪刀" and computer == "布"):
print("玩家出的{} \n电脑出的{}".format(player,computer))
print("您赢了!!!")
wins_count += 1
k+=1
elif (player == "布" and computer == "剪刀") or (player == "石头" and computer == "布") or (player == "剪刀" and computer == "石头"):
print("玩家出的{} \n电脑出的{}".format(player,computer))
print("您输了...")
fail_count += 1
k+=1
else:
print("您的输入有误!!!请重新输入...")
n-=1
continue #跳过此次循环
if n == 4: #玩到三局游戏进入判断
key = int(input("三局游戏已到....请问要继续游戏吗?(1:不了 2:继续): "))
if key == 1:
print("游戏结束...开始结算....您共进行了{}次游戏".format(k))
print("您赢了{}次 , 输了{} 次, 平局{}次".format(wins_count,fail_count,draw_count)) #打印结果
#重置信息
wins_count = 0 #赢得次数
fail_count = 0 #输的次数
draw_count = 0 #平局的次数
computer = "" #电脑的出拳
n = 1 #游戏局数
break
elif key == 2:
print("游戏继续...")
n = 1
continue
Python猜拳游戏
于 2022-09-22 18:41:45 首次发布