铅笔游戏代码无法输出获胜结果及测试报错排查
铅笔游戏代码问题:无法输出获胜语句且测试#16失败
问题概述
我开发的铅笔游戏无法打印最后一条判定玩家获胜的语句,同时测试#16失败,提示「当玩家输入要拿取的铅笔数量时,应输出恰好2行非空内容」。以下是相关信息:
开发需求
- 需校验输入合法性,限制玩家每次最多拿3支铅笔;输入错误时输出对应提示并重新请求输入。
- 游戏规则:拿最后一支铅笔的玩家落败,结束时需打印「Winner-name won!」。
- 输入错误场景及提示:
- 初始铅笔数非数字字符串:
The number of pencils should be numeric - 初始铅笔数为0:
The number of pencils should be positive - 输入负数:按场景1处理
- 所选玩家非John/Jack:
Choose between John and Jack - 拿取铅笔数非1/2/3:
Possible values: '1', '2' or '3' - 拿取铅笔数超过剩余数量:
Too many pencils were taken
- 初始铅笔数非数字字符串:
测试用例
提供6组测试示例,涵盖各类错误输入处理及获胜判定场景。
我的代码
print('How many pencils would you like to use:') pen_num: int = 0 first_player = 'John' second_player = 'Jack' while True: pen_num = input() if not pen_num.isnumeric(): print('The number of pencils should be numeric') continue if int(pen_num) == 0: print('The number of pencils should be positive') continue if int(pen_num) == 0: print('The number of pencils should be numeric') continue pen_num = int(pen_num) break print('Who will be the first (John, Jack):') while True: first_player = input() if not str(first_player) in ['John', 'Jack']: print('Choose between John and Jack') continue if first_player == 'Jack': second_player = 'John' break cur_turn: int = 1 cur_player = first_player while pen_num > 0: print('|' * pen_num) if cur_turn % 2 == 1: cur_player = first_player else: cur_player = second_player print(cur_player + "'s turn:") cut_num = 0 while True: cut_num = input() if not cut_num.isnumeric(): print("Possible values: '1', '2' or '3'") continue cut_num = int(cut_num) if not 1 <= cut_num <= 3: print("Possible values: '1', '2' or '3'") continue if cut_num > pen_num: print("Too many pencils were taken") continue break cur_turn += 1 pen_num -= cut_num if pen_num == 0: print(cur_player + " won!") else: break
测试报错
测试#16失败,提示「当玩家输入要拿取的铅笔数量时,应输出恰好2行非空内容」,并附上测试过程的输出记录。
问题修复分析
1. 核心问题点
- 游戏循环逻辑错误:外层
while pen_num > 0循环中,每次处理完一轮拿取后就执行else: break,导致游戏仅进行一轮就终止,既无法完成多轮游戏,也无法触发正确的获胜判定。 - 获胜判定逻辑错误:按照规则,拿最后一支铅笔的玩家落败,获胜者应为另一位玩家,但当前代码错误地将拿最后一支铅笔的玩家判定为胜者。
修复后的代码
print('How many pencils would you like to use:') pen_num: int = 0 first_player = 'John' second_player = 'Jack' # 初始铅笔数校验逻辑优化 while True: pen_num_input = input() if not pen_num_input.isnumeric(): print('The number of pencils should be numeric') continue pen_num = int(pen_num_input) if pen_num <= 0: print('The number of pencils should be positive') continue break # 玩家选择逻辑优化 print('Who will be the first (John, Jack):') while True: selected_player = input() if selected_player not in ['John', 'Jack']: print('Choose between John and Jack') continue first_player = selected_player second_player = 'Jack' if first_player == 'John' else 'John' break cur_turn: int = 1 while pen_num > 0: print('|' * pen_num) cur_player = first_player if cur_turn % 2 == 1 else second_player print(f"{cur_player}'s turn:") # 拿取数量校验 while True: cut_num_input = input() if not cut_num_input.isnumeric(): print("Possible values: '1', '2' or '3'") continue cut_num = int(cut_num_input) if not 1 <= cut_num <= 3: print("Possible values: '1', '2' or '3'") continue if cut_num > pen_num: print("Too many pencils were taken") continue break pen_num -= cut_num cur_turn += 1 # 正确判定获胜者:拿最后一支的玩家落败,胜者为另一位玩家 if pen_num == 0: winner = second_player if cur_player == first_player else first_player print(f"{winner} won!")
修复要点
- 移除外层循环中错误的
else: break,让游戏可以持续进行直到铅笔耗尽 - 修正获胜判定逻辑,将拿最后一支铅笔的玩家判定为落败者,另一位玩家为胜者
- 优化初始铅笔数和玩家选择的校验逻辑,消除重复判断和变量混淆
内容的提问来源于stack exchange,提问作者Mustafa Anandwala
相关产品推荐
相关产品推荐

