0

I have a list of players stored and I would like to flip between them. For example first, I ask player1 for input , and once he done I will ask player2 for input and after ask player3 , and ask player 1 again. and so on so forth.

It doesn't have to be only 3 player , it can be more or less than that

code:

fn = []
player_name = input("Enter player " + str(i+1) + " name: ")
fn.append(player_name)
print("1. Attack")
print("2. Charge")
print("3. Shield")
input(fn + " what would you like to do?  ")
print(str(minion.name) + " HP " + str(minion.hp))
uska = input(fn + " who would you like to attack? ")

so all the players are stored in "fn" but I can't concatenate it to the str. If I do fn[0] it only brings first name but I want it to flip between them

  • Does this answer your question? [Swapping 1 with 0 and 0 with 1 in a Pythonic way](https://stackoverflow.com/questions/1779286/swapping-1-with-0-and-0-with-1-in-a-pythonic-way) – Mike Scotty Feb 22 '20 at 22:07
  • @MikeScotty thank you, however, lets say I have 7 players not 2. how is that works then? –  Feb 22 '20 at 22:09
  • and players MUST be stored in a list –  Feb 22 '20 at 22:10
  • You could put all your players in a list and then [rotate](https://stackoverflow.com/questions/2150108/efficient-way-to-rotate-a-list-in-python) that list, or use a [deque](https://docs.python.org/3/library/collections.html#collections.deque) object (also mentioned in the linked question) – Mike Scotty Feb 22 '20 at 22:13
  • Your original questions asked how to toggle between player1 and player2, please do not change your question fundamentalliy, as it makes it hard or impossible to answer it or to link to matching duplicates. – Mike Scotty Feb 22 '20 at 22:15
  • I am sorry, I will change it now. –  Feb 22 '20 at 22:16

1 Answers1

0

The most straight-forward way to keep looping over a list of object indeterminately is to use a while-loop:

class Player:
    def __init__(self, name):
        self.name = name
    def greeting(self):
        return f"Hello, may name is {self.name}"

def game_loop(players):
    while True:
        for player in players:
            print(player.greeting())

players = [Player('Frank'), Player("Anna"), Player("Juan")]

game_loop(players)

Of course, the above will give you an infinite loop. So you are going to have to take care of breaking out of your loop, note, only a break directly within the while loop body, not inside the for-loop, will break the infinite loop.

An alternative which may make things easier to handle is to use itertools and a for-loop:

import itertools
for player in itertools.cycle(players):
    print(player.greeting())

This may be easier to work with, because then you can just break out of the single loop. But this is also an infinite loop, because cycle returns an infinite iterator.

Note, you could implement cycle yourself using a generator pretty-straightforwardly, the docs even contain these implementations (of course, in CPython, they aren't implemented in Python, they are implemented in C):

def cycle(iterable):
    # cycle('ABCD') --> A B C D A B C D A B C D ...
    saved = []
    for element in iterable:
        yield element
        saved.append(element)
    while saved:
        for element in saved:
              yield element

Notice, it still has this while True: for x in whatever: ... structure... It has just been encapsulated in an iterator, a generator in fact.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172