You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复石头剪刀布程序中的Break异常与循环失效Bug?

修复石头剪刀布程序的两个问题

问题1:平局后输入quit需两次才能退出

原因

出现平局时,代码会要求玩家重新输入出拳,但未对新输入的内容做退出检查,直接进入后续的出拳判断逻辑,导致输入quit被判定为无效操作,触发Please enter a valid move!提示。

修复

在平局后获取新的玩家输入后,立即添加退出检查:如果输入是quit或q,直接终止游戏循环。


问题2:落败后输入y重玩程序直接结束

原因

  1. 原重玩逻辑写在单局游戏的while循环内部,重置分数后循环已执行到末尾,无法重新进入游戏流程。
  2. 代码存在语法错误:if replay == "y"的位置破坏了前面if pwins > cwins的elif-else结构,导致逻辑分支混乱。

修复

  1. 新增外层循环,用play_again变量控制是否开启新一局游戏。
  2. 修复if-elif-else的结构错误,将重玩询问统一放在单局结束后处理。

修复后的完整代码

import random

print("Lets play Rock, Paper, Scissors...")
print("Type 'quit' to quit the game")

play_again = True
while play_again:
    pwins = 0
    cwins = 0
    winning_score = 2

    while pwins < winning_score and cwins < winning_score:
        print(f"Player wins:{pwins} | Computer wins: {cwins}")
        p1 = input("Player's move: ").lower()
        
        # 提前检查退出,避免生成电脑出拳的冗余操作
        if p1 == "quit" or p1 == "q":
            play_again = False
            break
            
        # 生成电脑出拳
        rand_num = random.randint(0,2)
        computer = "rock" if rand_num == 0 else "paper" if rand_num ==1 else "scissors"
        print(f"Computer chooses: {computer}")

        # 处理平局情况
        if p1 == computer:
            print("It's a tie...")
            p1 = input("Player's move: ").lower()
            # 平局后输入quit直接退出
            if p1 == "quit" or p1 == "q":
                play_again = False
                break
            # 平局后重新生成电脑出拳
            rand_num = random.randint(0,2)
            computer = "rock" if rand_num == 0 else "paper" if rand_num ==1 else "scissors"
            print(f"Computer chooses: {computer}")

        # 检查是否触发退出
        if not play_again:
            break

        # 判断胜负
        if p1 == "rock":
            cwins +=1 if computer == "paper" else pwins +=1
            print("Computer wins" if computer == "paper" else "Player wins")
        elif p1 == "paper":
            cwins +=1 if computer == "scissors" else pwins +=1
            print("Computer wins" if computer == "scissors" else "Player wins")
        elif p1 == "scissors":
            cwins +=1 if computer == "rock" else pwins +=1
            print("Computer wins" if computer == "rock" else "Player wins")
        else:
            print("Please enter a valid move!")
            continue

    if not play_again:
        break

    # 单局结束后展示分数并询问重玩
    print(f"Final score... Player: {pwins} | Computer: {cwins}")
    if pwins > cwins:
        replay = input("Congrats, you won! Do you want to play again? (y/n) ")
    elif pwins < cwins:
        replay = input("You lost...Do you want to play again? (y/n) ")
    else:
        replay = input("It's a tie! Do you want to play again? (y/n) ")

    play_again = replay.lower() == "y"
    if not play_again:
        print("Loser!")

额外优化

  • 将退出检查提前,减少不必要的电脑出拳生成操作
  • 平局后同步重新生成电脑出拳,符合真实游戏规则
  • 简化胜负判断的代码逻辑,提升可读性

内容的提问来源于stack exchange,提问作者ughhh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 03:07:52