python - What change needs to be made to the main function so that each player has exactly three turns? -
currently players have 1 turn each , winner of game never displayed. change needs made main function give each player exactly 3 turns?
def main(): display_welcome() #1 number_of_turns = 3 score_player1 = 0 score_player2 = 0 name_player1 = "olivia" name_player2 = "ned" turn_num = 1 first_player_num = random.randrange(1, number_of_turns + 1) if first_player_num == 2: temp = name_player1 name_player1 = name_player2 name_player2 = temp score1 = have_one_turn(turn_num, name_player1) score2 = have_one_turn(turn_num, name_player2) score_player1 = score_player1 + score1 score_player2 = score_player2 + score2 if turn_num < number_of_turns: display_turn_results(name_player1, score_player1, name_player2, score_player2, false) #10 else: display_turn_results(name_player1, score_player1, name_player2, score_player2, true)
try following code ::
def main(): display_welcome() #1 number_of_turns = 3 score_player1 = score_player2 = 0 name_player1 = "olivia" name_player2 = "ned" first_player_num = random.randrange(1, 2) if first_player_num == 2: temp = name_player1 name_player1 = name_player2 name_player2 = temp turn_num in xrange(3): score1 = have_one_turn(turn_num, name_player1) score2 = have_one_turn(turn_num, name_player2) score_player1 = score_player1 + score1 score_player2 = score_player2 + score2 if turn_num+1 < number_of_turns: display_turn_results(name_player1, score_player1, name_player2, score_player2, false) #10 else: display_turn_results(name_player1, score_player1, name_player2, score_player2, true)
if part of main function can separated different one, recursive function call can used.
def main(): #display function , selecting player play first. game(score1, score2, name_player1, name_player2)
the game function can called recursively
def game(score1, score2, name_player1, name_player2, turn_num=1): #calculating score number_of_turns = 3 turn_num = turn_num+1 if turn_num < number_of_turns: display_turn_results(name_player1, score_player1, name_player2, score_player2, false) game(score_player1, score_player2, name_player1, name_player2, turn_num) else: display_turn_results(name_player1, score_player1, name_player2, score_player2, true)
Comments
Post a Comment