Zybooks 18.7骰子游戏项目:play_multiple_rounds函数无限循环问题求助
问题背景
完成Zybooks 18.7骰子游戏项目的步骤1-7,卡在步骤8。play_multiple_rounds函数存在问题,导致程序触发**"Program generated too much output. Output restricted to 50000 characters. Check program for any unterminated loops generating output."**错误,以下是原代码及错误输出:
原代码
import random def first_roll(): d1 = random.randint(1, 6) d2 = random.randint(1, 6) sum_of_die = d1 + d2 if sum_of_die == 7 or sum_of_die == 11: print("Rolled dice: ({},{}).".format(d1, d2), sum_of_die, "wins") point = 1 elif sum_of_die == 2 or sum_of_die == 3 or sum_of_die == 12: print("Rolled dice: ({},{}).".format(d1, d2), sum_of_die, "loses") point = -1 else: point = sum_of_die print("Point = ", point) return point def next_rolls(point): end = 1 result = 0 while end == 1: d1 = random.randint(1, 6) d2 = random.randint(1, 6) sum_of_die = d1 + d2 if sum_of_die == 7: print("Rolled dice: ({},{}).".format(d1, d2), sum_of_die, "loses") result = -1 end = 0 elif sum_of_die == point: print("Rolled dice: ({},{}).".format(d1, d2), sum_of_die, "wins") result = 1 end = 0 else: print("Rolled dice: ({},{}).".format(d1, d2), ".", "Roll again.") continue return result def play_one_round(credits): outcome = first_roll() if outcome <= 1: credits = credits + outcome else: outcome2 = next_rolls(outcome) print(outcome2) credits = credits + outcome2 return credits def play_multiple_rounds(rounds, credits): for x in range(0,rounds): while play_one_round(credits) >= 1: print("Round", rounds,":", "Credits", credits) return credits if __name__ == '__main__': first_roll()
错误输出
Point = 4
Rolled dice: (1,5). . Roll again.
Rolled dice: (3,4). 7 losesRound 50 : Credits 20
Point = 9
Rolled dice: (3,2). . Roll again.
Rolled dice: (6,2). . Roll again.
Rolled dice: (3,4). 7 losesRound 50 : Credits 20
Rolled dice: (1,2). 3 loses
Round 50 : Credits 20
Point = 9
Rolled dice: (1,1). . Roll again.
Rolled dice: (1,6). 7 losesRound 50 : Credits 20
Point = 8
Rolled dice: (1,5). . Roll again.
Rolled dice: (6,6). . Roll again.
Rolled dice: (4,3). 7 losesRound 50 : Credits 20
Rolled dice: (1,2). 3 loses
Round 50 : Credits 20
Point = 6
Rolled dice: (3,6). . Roll again.
Rolled dice: (2,5). 7 loses
-1...
错误原因分析
- 无限循环触发:
play_multiple_rounds函数中,for循环内部嵌套了while循环,且调用play_one_round(credits)时没有更新credits变量的实际值,导致while的条件play_one_round(credits) >=1会反复调用新的轮次,永远无法退出循环。 - 轮次显示错误:打印轮次时使用了总轮数
rounds,而非当前循环的轮次x+1(因为range(0, rounds)从0开始计数)。 - credits未正确更新:每次调用
play_one_round后,没有将返回的新credits值赋值给原变量,导致credits始终保持初始值。
修复后的代码
import random def first_roll(): d1 = random.randint(1, 6) d2 = random.randint(1, 6) sum_of_die = d1 + d2 if sum_of_die == 7 or sum_of_die == 11: print("Rolled dice: ({},{}).".format(d1, d2), sum_of_die, "wins") point = 1 elif sum_of_die == 2 or sum_of_die == 3 or sum_of_die == 12: print("Rolled dice: ({},{}).".format(d1, d2), sum_of_die, "loses") point = -1 else: point = sum_of_die print("Point = ", point) return point def next_rolls(point): end = 1 result = 0 while end == 1: d1 = random.randint(1, 6) d2 = random.randint(1, 6) sum_of_die = d1 + d2 if sum_of_die == 7: print("Rolled dice: ({},{}).".format(d1, d2), sum_of_die, "loses") result = -1 end = 0 elif sum_of_die == point: print("Rolled dice: ({},{}).".format(d1, d2), sum_of_die, "wins") result = 1 end = 0 else: print("Rolled dice: ({},{}).".format(d1, d2), ".", "Roll again.") continue return result def play_one_round(credits): outcome = first_roll() if outcome <= 1: credits = credits + outcome else: outcome2 = next_rolls(outcome) print(outcome2) credits = credits + outcome2 return credits def play_multiple_rounds(rounds, credits): for x in range(rounds): # 调用play_one_round并更新credits的值 credits = play_one_round(credits) # 打印当前轮次和最新credits,轮次从1开始计数 print(f"Round {x+1}: Credits {credits}") return credits if __name__ == '__main__': # 示例:玩5轮,初始credits为20 final_credits = play_multiple_rounds(5, 20) print(f"Final credits: {final_credits}")
修复说明
- 移除无限循环:去掉
for循环内部的while,改为每轮调用一次play_one_round,保证循环次数严格等于指定的rounds。 - 更新credits变量:将
play_one_round的返回值赋值给credits,确保每轮结束后credits会正确更新。 - 修正轮次显示:使用
x+1作为当前轮次,符合用户对轮次从1开始的预期。 - 完善主函数:修改
main函数为调用play_multiple_rounds并传入测试参数,方便验证功能。
内容的提问来源于stack exchange,提问作者Caleb Jordan

