请在上述代码增加统计并显示胜局,败局,平局的局数
时间: 2024-10-25 08:15:13 浏览: 16
py代码-pyton练习--双方进行三局PK,每局判定胜负,三局两胜制
为了增加对胜局、败局和平局的统计,你可以添加三个变量来跟踪这些结果,并在每次游戏结束后更新它们。这里是修改后的代码:
```python
import random
# 初始化计数器
wins = 0
losses = 0
ties = 0
def get_player_choice():
while True:
choice = input("请输入你的选择 (S for 石头, J for 剪刀, B for 布, Q for 退出): ").lower()
if choice in ['s', 'j', 'b', 'q']:
return choice
else:
print("无效的选择,请输入S, J, B或Q。")
def get_computer_choice():
choices = ['s', 'j', 'b']
return random.choice(choices)
def play_game():
global wins, losses, ties
player = get_player_choice()
computer = get_computer_choice()
if player == 'q':
print("游戏结束,感谢参与!")
return
print(f"你出了{player}, 对手出了{computer}.")
if player == computer:
result = "平局"
ties += 1
elif (player == 's' and computer == 'j') or \
(player == 'j' and computer == 'b') or \
(player == 'b' and computer == 's'):
result = "你赢了!"
wins += 1
else:
result = "你输了!"
losses += 1
print(f"{result}! 当前记录: 赢{wins}次, 跳{losses}次, 平{ties}局")
play_game()
```
现在当你玩完一局游戏后,程序会显示胜局、败局和平局的具体数目。如果你想知道更多关于如何进一步扩展或优化这个统计功能的问题,可以继续提问:
阅读全文