问题
Im trying to get this loop to work, but it just refuses to work. Any help would be very much appreciated.
while player1_score < 100 and player2_score < 100 and player3_score < 100 and player4_score < 100:
while player_turn != playerholder:
dice_rolling()
scoring()
score_awarding()
player_turn = player_turn + 1
if player_turn == playerholder:
player_turn == 1
What im trying to do is get the number of players present (playerholder) and then keep player turn within the bounds of it i.e repeating till someone gains a score of 100 or more. Just ask if you need any more code :) Thanks in advance.
EDIT: Everyone seems to be confused as to what I'm asking so I'm going to try explain it more elaborately.
So the game has from 2 to 4 players, and what they are trying to do is get a score of 100 or more. Since there are multiple players, I want to try and have a repeating structure to efficiently use the functions I have created over and over. With the current code, it will score Players like it should up until the last player, where the code stops completely and nothing is presented. What I would like to do is get the code to repeat infinitely until the required score is gained, as in it will keep cycling through the players with no errors. I have tried multiple times with different loops but I cannot get it to work. Hopefully this is a little bit clearer to everyone. Sorry about the unorganized/unclear question, relatively new to StackOverflow.
EDIT: Problem has been solved
回答1:
Your code probably doesn't work as intended because instead
of assigning 1
to the player_turn
you do this:
player_turn == 1
when it should be this:
player_turn = 1
回答2:
As an aside, you should be keeping your players in some sort of list and iterating through them, allowing them each to take a turn in their iteration. Something like:
players = [player1,player2,player3,player4]
for player in players:
if player.score >= 100: win(player)
dice_rolling()
player.score += scoring()
But of course this only works if you have some sort of class Player
in your code (which you should).
class Player(object):
def __init__(self,name):
self.name = name
self.score = 0
# what else does a Player do?
In fact you can probably make a Game
class as well!
class Game(object):
def __init__(self,players):
self.players = players # a list of players
self.running = False
def dice_rolling(self):
diceroll = sum(random.randint(1,6) for _ in range(2))
def scoring(self):
return score # I don't know how you're doing this so...
def win(self,player):
print("{} wins!".format(player.name))
def run(self):
self.running = True
while self.running:
for player in self.players:
roll = dice_rolling()
player.score += scoring()
if player.score >= 100:
self.running = False
win(player)
Game().run()
回答3:
You could try something using a list structure.
It would look like this if there were 4 players:
turns = [0,1,2,3,4,0,1,2,3,4]
#this would add another turn for player 0, to remove you just use del
turns.append(0)
#This would remove a winner from your turn stack while preserving the order
for i in turns:
if i ==4:
del i
#You could also use a dictionary if you don't like list indicies or numbers:
Players = {"blue":3, red":4, "green":2, "orange":1}
turn = [4,2,1,3,4,2,1,3]
#If there is also a maximum number of turns, you can have that hard coded also:
for i in range(max):
turn.append(1)
turn.append(2)
turn.append(3)
turn.append(4)
Truthfully, I don't really understand what your question but hopefully this may have answered it wholly or in part.
来源:https://stackoverflow.com/questions/22545530/how-to-create-a-loop-for-multiple-players-turn