0

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)
3
  • Where is the code for take_turn and get_next_player Commented Oct 23, 2017 at 1:59
  • 1
    @VinceW.: that code is irrelevant Commented Oct 23, 2017 at 2:01
  • if is_user_turn is True: can become if is_user_turn: and you needn't do the check in elif is_user_turn is False:. It can only be true or false, so just else: will suffice. Commented Oct 23, 2017 at 2:09

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Can't believe I didn't see that!
@mhawke, the assertion that one user will always have less than 100 points seems to follow from the never ending loop. Is that evident in the code somewhere though? The reason I asked to see the other functions was because it was not clear how points were being assigned and without knowing the rules of the game it seemed plausible that at some point both players would have more than 100 points.
0

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?

Comments

0

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.

1 Comment

Thanks! I'll definitely use this next time I run into this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.