目前,每个玩家只有一个转弯,并且永远不会显示游戏的赢家。需要对这个主要功能进行哪些更改才能让每个玩家完全三次?
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)
答案 0 :(得分:-1)
尝试以下代码::
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
for 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)
如果main函数的一部分可以分成另一部分,则可以使用递归函数调用。
def main():
#display function and selecting player to play first.
game(score1, score2, name_player1, name_player2)
游戏功能可以称为递归
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)