I was wondering if anyone had any ideas on how to alternate between two players until a specific condition has been met? I had while statements in mind but I'm not sure if I can actually put it into practise.
Any ideas would be appreciated!
I was wondering if anyone had any ideas on how to alternate between two players until a specific condition has been met? I had while statements in mind but I'm not sure if I can actually put it into practise.
Any ideas would be appreciated!
from itertools import cycle
for player in cycle(["player1", "player2"]):
do_turn(player)
if game_over():
break
This is nice because it is extendable to any number of players.
In the very broadest terms, as a sort of guessing game (first correct answer wins) and assuming a Player
class with name
attribute and an answer(question)
method for taking user input:
active, passive = Player("One"), Player("Two") # create two Players
while True:
if active.answer(question) == correct_answer: # active player guesses
break # correct answer, leave the loop
active, passive = passive, active # switch Players
print("{0.name} was correct.".format(active)) # announce winner