简体   繁体   中英

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. 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.

Instead of or use 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.

You while loop will only end if both computer_pt >= GAME_END_POINTS and user_pt >= GAME_END_POINTS . 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. Print variable in loop is a common way to debug your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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