Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my variable not updating in the while loop?

may I ask what’s wrong with my code as the variable is not working like it’s supposed to in the while loop. It’s supposed to go like player 1, 2 and 3 but it keep on repeating as player 1 instead. How can I fix it?

def display_game_options(player):
    """
    Prints the game options depending on if a player's score is
    >= 14.

    Arguments:
      - player: A player dictionary object
    """
    player = {1: {'name': 'Player 1', 'score': 0, 'stayed': False, 'at_14': False, 'bust': False},
              2: {'name': 'Player 2', 'score': 0, 'stayed': False, 'at_14': False, 'bust': False},
              3: {'name': 'Player 3', 'score': 0, 'stayed': False, 'at_14': False, 'bust': False},
              4: {'name': 'Player 4', 'score': 0, 'stayed': False, 'at_14': False, 'bust': False}}

    n = 0
    playing = True

    while playing is True:
        print("--------", player[n + 1]['name'], "'s turn--------")
        print(player[n + 1]['name'], "'s score: ", player[n + 1]['score'])
        print("1. Roll")
        print("2. Stay")
        decison = input("")
        if player[n + 1]['score'] >= 14:
            print("3. Roll one")

    raise NotImplementedError

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It states "player 1" continuously because it uses n to figure that out, and n never changes in your loop.

If you want to cycle through the players, you’ll need to modify n with something like (at the end of the loop, but within it):

n = (n + 1) % 4 # gives 0, 1, 2, 3, 0, 1, ...

For example, you can see how this works in the following transcript:

>>> n = 0
>>> for _ in range(10):
...     print(n+1)
...     n = (n + 1) % 4
...
1
2
3
4
1
2
3
4
1
2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading