Codecademy循环咖啡聊天机器人:退出词功能异常求助
问题描述
我是Codecademy平台《Build Chatbots with Python》技能路径的初学者,正在完成Looping Coffee Chatbot项目的第13步附加任务——给聊天机器人添加识别"stop""bye"等退出词的功能,要求在任意步骤输入这些词都能终止订单。但现在输入退出词时,不会触发退出逻辑(打印"Sure, feel free to come back anytime!"并终止函数),反而会调用utils.py里的print_message函数。
相关代码如下:
script.py
from utils import print_message, get_size, order_latte exit_words = ["stop", "bye"] def coffee_bot(): print('Welcome to the cafe!') order_drink = "y" drinks = [] while order_drink in ["y", "yes", "affirmative", "amen", "fine", "good", "okay", "true", "yea"]: size = get_size() if size in exit_words: print("Sure, feel free to come back anytime!") return drink_type = get_drink_type() if drink_type in exit_words: print("Sure, feel free to come back anytime!") return drink = '{} {}'.format(size, drink_type) print('Alright, that\'s a {}!'.format(drink)) drinks.append(drink) while True: order_drink = input("Would you like to order another drink? (y/n) \n> ") if order_drink in ["y", "n", "yes", "affirmative", "amen", "fine", "good", "okay", "true", "yea", "nah", "nay", "nix"]: break elif order_drink in exit_words: print("Sure, feel free to come back anytime!") return print("Okay, so I have:") for drink in drinks: print("-", drink) name = input('Can I get your name please? \n> ') if name in exit_words: print("Sure, feel free to come back anytime!") return print('Thanks, {}! Your order will be ready shortly.'.format(name)) def get_drink_type(): res = input('What type of drink would you like? \n[a] Brewed Coffee \n[b] Mocha \n[c] Latte \n> ') if res == 'a': return 'brewed coffee' elif res == 'b': return order_mocha() elif res == 'c': return order_latte() else: print_message() return get_drink_type() # Define new functions here! def order_mocha(): while True: res = input("Would you like to try our limited-edition peppermint mocha? \n[a] Sure! \n[b] Maybe next time! \n> ") if res == "a": return "peppermint mocha" elif res == "b": return "mocha" print_message() coffee_bot()
utils.py
exit_words = ["stop", "bye"] def print_message(): print('I\'m sorry, I did not understand your selection. Please enter the corresponding letter for your response.') def get_size(): res = input('What size drink can I get for you? \n[a] Small \n[b] Medium \n[c] Large \n> ') if res == 'a': return 'small' elif res == 'b': return 'medium' elif res == 'c': return 'large' else: print_message() return get_size() def order_latte(): res = input('And what kind of milk for your latte? \n[a] 2% milk \n[b] Non-fat milk \n[c] Soy milk \n> ') if res == 'a': return 'latte' elif res == 'b': return 'non-fat latte' elif res == 'c': return 'soy latte' else: print_message() return order_latte()
问题原因
所有处理输入的函数(get_size、get_drink_type、order_mocha、order_latte)都没有优先检查输入是否为退出词,而是直接将非指定字母的输入判定为无效,调用print_message并递归重试。这导致退出词根本无法返回给上层的coffee_bot函数,自然触发不了退出逻辑。
解决方案
在每个处理输入的函数中,先判断输入是否属于退出词,如果是则直接返回该词,让上层的coffee_bot能捕获并执行退出逻辑。同时统一用lower()处理输入,兼容大小写差异。
修改后的代码如下:
修改后的utils.py
exit_words = ["stop", "bye"] def print_message(): print('I\'m sorry, I did not understand your selection. Please enter the corresponding letter for your response.') def get_size(): res = input('What size drink can I get for you? \n[a] Small \n[b] Medium \n[c] Large \n> ') # 优先检查退出词 if res.lower() in exit_words: return res.lower() if res == 'a': return 'small' elif res == 'b': return 'medium' elif res == 'c': return 'large' else: print_message() return get_size() def order_latte(): res = input('And what kind of milk for your latte? \n[a] 2% milk \n[b] Non-fat milk \n[c] Soy milk \n> ') # 优先检查退出词 if res.lower() in exit_words: return res.lower() if res == 'a': return 'latte' elif res == 'b': return 'non-fat latte' elif res == 'c': return 'soy latte' else: print_message() return order_latte()
修改后的script.py
from utils import print_message, get_size, order_latte exit_words = ["stop", "bye"] def coffee_bot(): print('Welcome to the cafe!') order_drink = "y" drinks = [] while order_drink in ["y", "yes", "affirmative", "amen", "fine", "good", "okay", "true", "yea"]: size = get_size() if size in exit_words: print("Sure, feel free to come back anytime!") return drink_type = get_drink_type() if drink_type in exit_words: print("Sure, feel free to come back anytime!") return drink = '{} {}'.format(size, drink_type) print('Alright, that\'s a {}!'.format(drink)) drinks.append(drink) while True: order_drink = input("Would you like to order another drink? (y/n) \n> ") # 优先检查退出词 if order_drink.lower() in exit_words: print("Sure, feel free to come back anytime!") return if order_drink.lower() in ["y", "n", "yes", "affirmative", "amen", "fine", "good", "okay", "true", "yea", "nah", "nay", "nix"]: order_drink = order_drink.lower() break else: print_message() # 用户选择不再点单时终止循环 if order_drink == 'n': break print("Okay, so I have:") for drink in drinks: print("-", drink) name = input('Can I get your name please? \n> ') if name.lower() in exit_words: print("Sure, feel free to come back anytime!") return print('Thanks, {}! Your order will be ready shortly.'.format(name)) def get_drink_type(): res = input('What type of drink would you like? \n[a] Brewed Coffee \n[b] Mocha \n[c] Latte \n> ') # 优先检查退出词 if res.lower() in exit_words: return res.lower() if res == 'a': return 'brewed coffee' elif res == 'b': return order_mocha() elif res == 'c': return order_latte() else: print_message() return get_drink_type() def order_mocha(): while True: res = input("Would you like to try our limited-edition peppermint mocha? \n[a] Sure! \n[b] Maybe next time! \n> ") # 优先检查退出词 if res.lower() in exit_words: return res.lower() if res == "a": return "peppermint mocha" elif res == "b": return "mocha" print_message() coffee_bot()
内容的提问来源于stack exchange,提问作者Noah
相关产品推荐
相关产品推荐

