Python灵性动物测验程序故障求助:执行中断问题排查
灵性动物测试程序故障排查
我开发了一个基于两道题答案选择灵性动物的Python程序,要求main()外至少包含4个函数。目前问题环节能正常运行,但程序执行后会中断,附上代码请求排查故障原因:
def main(): colors() foods() results() def colors(): colors = float(int(input('What is your favorite color of the following choices:\n\nPress 1 for Pink\nPress 2 for Blue\nPress 3 for Green\n\n'))) if colors != 1 and colors != 2 and colors != 3: colors = float(int(input('Invalid option. Try again:\n'))) def foods(): foods = float(int(input('Nice! Now what is your favorite food of the following choices:\n\nPress 1 for Steak\nPress 2 for Chicken\nPress 3 for Seafood\n\n'))) if foods != 1 and foods != 2 and foods != 3: foods = float(int(input('Invalid option. Try again:\n'))) def results(): if (colors == 1) and (foods == 1): print('Your spirit animal is...', Eagle) elif (colors == 1) and (foods == 2): print('Your spirit animal is...', Wolf) elif (colors == 1) and (foods == 3): print('Your spirit animal is...', Horse) elif (colors == 2) and (foods == 1): print('Your spirit animal is...', Deer) elif (colors == 2) and (foods == 2): print('Your spirit animal is...', Tiger) elif (colors == 2) and (foods == 3): print('Your spirit animal is...', Lion) elif (colors == 3) and (foods == 1): print('Your spirit animal is...', Turtle) elif (colors == 3) and (foods == 2): print('Your spirit animal is...', Hawk) elif (colors == 3) and (foods == 3): print('Your spirit animal is...', Bear) def animals(): Eagle = print('Eagle\n\nYou are strong, brave, creative, and insightful') Wolf = print('Wolf\n\nYou have a high sense of loyalty and communication') Horse = print('Horse\n\nYou embody independence and freedom') Deer = print('Deer\n\nYou are gentle, strong, and love unconditionally') Tiger = print('Tiger\n\nYou are strong, beautiful and independent') Lion = print('Lion\n\nYou are a brave and outspoken leader') Turtle = print('Turtle\n\nYou are wise and patient') Hawk = print('Hawk\n\nYou are a symbol of clarity and truth') Bear = print('Bear\n\nYou are strong, courageous and wise') main()
故障原因分析
- 变量作用域错误:
colors()和foods()函数中定义的colors、foods是局部变量,results()函数无法访问这些变量,执行时会触发NameError。 - 输入验证不完整:当前只做了一次无效输入检查,如果用户再次输入错误值,程序依然会接受无效值,应该用循环持续验证直到输入合法。
- 动物描述函数未调用且逻辑错误:
animals()函数从未被执行,而且里面将print()的返回值(None)赋值给变量,这些变量也从未被results()使用。 - 未定义的动物变量:
results()中使用的Eagle、Wolf等变量未定义,执行时会触发NameError。
修正后的代码示例
def main(): color_choice = colors() food_choice = foods() results(color_choice, food_choice) def colors(): while True: try: choice = int(input('What is your favorite color of the following choices:\n\nPress 1 for Pink\nPress 2 for Blue\nPress 3 for Green\n\n')) if choice in (1, 2, 3): return choice else: print('Invalid option. Try again:') except ValueError: print('Please enter a valid number.') def foods(): while True: try: choice = int(input('Nice! Now what is your favorite food of the following choices:\n\nPress 1 for Steak\nPress 2 for Chicken\nPress 3 for Seafood\n\n')) if choice in (1, 2, 3): return choice else: print('Invalid option. Try again:') except ValueError: print('Please enter a valid number.') def get_animal_description(animal_name): descriptions = { 'Eagle': 'Eagle\n\nYou are strong, brave, creative, and insightful', 'Wolf': 'Wolf\n\nYou have a high sense of loyalty and communication', 'Horse': 'Horse\n\nYou embody independence and freedom', 'Deer': 'Deer\n\nYou are gentle, strong, and love unconditionally', 'Tiger': 'Tiger\n\nYou are strong, beautiful and independent', 'Lion': 'Lion\n\nYou are a brave and outspoken leader', 'Turtle': 'Turtle\n\nYou are wise and patient', 'Hawk': 'Hawk\n\nYou are a symbol of clarity and truth', 'Bear': 'Bear\n\nYou are strong, courageous and wise' } return descriptions.get(animal_name, 'Unknown animal') def results(color, food): animal_map = { (1, 1): 'Eagle', (1, 2): 'Wolf', (1, 3): 'Horse', (2, 1): 'Deer', (2, 2): 'Tiger', (2, 3): 'Lion', (3, 1): 'Turtle', (3, 2): 'Hawk', (3, 3): 'Bear' } animal = animal_map.get((color, food)) print(f'Your spirit animal is... {animal}') print(get_animal_description(animal)) main()
内容的提问来源于stack exchange,提问作者Cody Dannelly
相关产品推荐
相关产品推荐

