Why is my variable not updating in the while loop?
Question:
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
Answers:
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
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
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