1

The code I am currently using,

if p == players[1]:
    p = players[2]
elif p == players[2]:
    p = players[3]
elif p == players[3]:
    p = players[4]
elif p == players[4]:
    p = players[5]
else:
    # et cetera

p holds the current player. Everyone playing is in a list called players. I did this once with a modulo but now I can't remember how.

1
  • Instead of setting the variable to the player, set the variable to the player index. Then you can simply increment the variable modulo len(players) Commented Nov 3, 2020 at 2:14

2 Answers 2

1

Your idea to use modulo is correct, here is one way to approcah it:

def swap_players(ndx):
    return players[(ndx+1) % len(players)]

players = ['a', 'b', 'c']

for n in range(10):
    p = swap_players(n)
    print(p)

output:

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

Comments

1

Use index if the next player is i+1 th player, as follows.

i = players.index(p)
p = players[i+1]

1 Comment

just add if statement for the last element

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.