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

Python猜拳游戏while循环条件异常:无法在一方得分达3时终止

猜拳游戏循环终止条件问题分析

问题描述

编写的Python猜拳游戏期望当玩家(score_player)或电脑(score_computer)任意一方得分达到3时终止该局游戏,但实际运行时,游戏会持续到双方得分均≥3甚至一方得分超过3才停止,询问while循环条件未正确执行的原因。

原代码:

import random

while True:
    score_player = 0
    score_computer = 0

    while score_computer <= 2 or score_player <= 2:
        choices = ['rock', 'scissors', 'paper']
        player = None
        computer = random.choice(choices)

        while player not in choices:
            player = input('rock,scissors or paper? : ').lower()

        print('computer:', computer)
        print('player: ', player)

        if player == computer:
            print('You are tied!')
        elif player == 'rock':
            if computer == 'scissors':
                score_player += 1
            elif computer == 'paper':
                score_computer += 1

        elif player == 'scissors':
            if computer == 'paper':
                score_player += 1
            elif computer == 'rock':
                score_computer += 1

        elif player == 'paper':
            if computer == 'rock':
                score_player += 1
            elif computer == 'scissors':
                score_computer += 1

        print('Score_player : ', score_player)
        print('Score_computer', score_computer)


    playe_again = input('Do you want to continue? (Yes/No) :').lower()
    #while playe_again == 'yes' or 'no':
    if playe_again != 'yes':
        break
print('I hope you have enjoyed the game!')

问题原因

核心问题出在内层while循环的条件判断逻辑错误:

while score_computer <= 2 or score_player <= 2:

这里使用or运算符的含义是:只要电脑得分≤2 或者 玩家得分≤2,循环就会继续执行。也就是说,只有当电脑得分>2 并且 玩家得分>2时,循环才会终止——这完全和你“任意一方到3分就终止”的需求相反。

你的需求是:只要有一方得分达到3(即>2)就停止该局游戏,因此循环应该在双方得分都未达到3时才继续,需要把or替换为and:

while score_computer <= 2 and score_player <= 2:

这样只要任意一方得分≥3,循环条件就不成立,游戏会立即终止当前对局。

修正后的代码

import random

while True:
    score_player = 0
    score_computer = 0

    # 修改循环条件为and,双方都未到3分时才继续游戏
    while score_computer <= 2 and score_player <= 2:
        choices = ['rock', 'scissors', 'paper']
        player = None
        computer = random.choice(choices)

        while player not in choices:
            player = input('rock,scissors or paper? : ').lower()

        print('computer:', computer)
        print('player: ', player)

        if player == computer:
            print('You are tied!')
        elif player == 'rock':
            if computer == 'scissors':
                score_player += 1
            elif computer == 'paper':
                score_computer += 1

        elif player == 'scissors':
            if computer == 'paper':
                score_player += 1
            elif computer == 'rock':
                score_computer += 1

        elif player == 'paper':
            if computer == 'rock':
                score_player += 1
            elif computer == 'scissors':
                score_computer += 1

        print('Score_player : ', score_player)
        print('Score_computer', score_computer)


    play_again = input('Do you want to continue? (Yes/No) :').lower()
    if play_again != 'yes':
        break
print('I hope you have enjoyed the game!')

额外细节修正

原代码中有一个拼写错误:playe_again改为play_again,虽然不影响运行,但修正后代码更规范。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 12:27:41