简体   繁体   English

我的“ while循环”未按预期工作

[英]My “while loop” not working as expected

I am a new coder, sorry if my question is bad or I am not following proper etiquette! 我是新编码员,如果我的问题不好或者我没有遵守正确的礼节,对不起!

I am designing a basic program that rolls dice. 我正在设计一个掷骰子的基本程序。 It is supposed to roll dice until the total points of either the computer or the user equals 100. However, even though my point totaler is working, the loop won't end. 应该掷骰子直到计算机或用户的总分等于100。但是,即使我的总分计正在运行,循环也不会结束。 Anyone know why this is? 有人知道为什么吗? Thank you! 谢谢!


def main():
        GAME_END_POINTS = 100
        COMPUTER_HOLD = 10
        is_user_turn = True
        user_pt = 0
        computer_pt = 0
        welcome()
        while computer_pt < GAME_END_POINTS or user_pt < GAME_END_POINTS:
            print_current_player(is_user_turn)
            if is_user_turn is True:
                user_pt = user_pt + take_turn(is_user_turn, COMPUTER_HOLD)
            elif is_user_turn is False:
                computer_pt = computer_pt + take_turn(is_user_turn, COMPUTER_HOLD)
            report_points(user_pt, computer_pt)
            is_user_turn = get_next_player(is_user_turn)

The condition is always True because either the computer or the user will have a points total less than 100. 该条件始终为True因为计算机用户的总积分小于100。

Instead of or use and : 代替or使用and

while computer_pt < GAME_END_POINTS and user_pt < GAME_END_POINTS:

Now the loop will continue only when both the user and the computer have a points total less than 100. As soon as one of them has more than 100 the condition will be be False and the loop will terminate. 现在,循环只会继续当两个用户和计算机有一个点一旦总共不到100作为其中之一已超过100的情况将是False ,循环终止。

You while loop will only end if both computer_pt >= GAME_END_POINTS and user_pt >= GAME_END_POINTS . 仅当computer_pt >= GAME_END_POINTS and user_pt >= GAME_END_POINTS都结束时,while循环才会结束。 Are you sure that those two variables satisfy those two conditions? 您确定这两个变量满足这两个条件吗?

you can print computer_pt and user_pt in the loop to see what happened in this two variable, then you will find the answer by your self. 您可以在循环中打印computer_pt和user_pt来查看这两个变量发生了什么,然后您将自行找到答案。 Print variable in loop is a common way to debug your code. 循环打印变量是调试代码的常用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM