Ahmed
Ahmed

Reputation: 129

How to make turns between players

Let's say i have a list of players, list_of_players = [1,2,3,4], each number represents a player. Basically what want i to do is pass each player through while run: each time run becomes True(in the program run becomes true when ever you press enter). So for the first turn i want player to become 1 and when the loop breaks and you press enter again i want player to become 2 etc. when it reaches player = 4 i want 4 to pass through the loop and reset player = 4 to player = 1(the second turn). I have tried many ways but it won't work. So the question is how can i do this? All kind of help is appreciated.

    while run:
        message.append(display_turn(player))
        new_position = return_postion(player) + int(rolled_number[-1])
        updating_position(player,new_position)
        if level_1[return_postion(player)]== 15:
            message.append('You landed on a trap')
            updating_health(player,0,15)
        break

Upvotes: 0

Views: 1289

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191748

You're looking for the modulo operator

max_players = 4

player = 0

while True:
    print("player {} turn".format(player+1))

    input("enter for next {}".format("player" if player + 1 < max_players else "round"))
    player = (player + 1) % max_players

Output

player 1 turn
enter for next player 
player 2 turn
enter for next player 
player 3 turn
enter for next player 
player 4 turn
enter for next round 
player 1 turn
enter for next player 
player 2 turn
enter for next player 
player 3 turn
enter for next player 
player 4 turn
enter for next round  
...

Upvotes: 2

Related Questions