You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python导入自定义快餐订单模块时提示food_choice未定义错误

Python快餐订单程序自定义模块导入后food_choice未定义报错修复

报错触发原因

运行脚本执行到module5.get_total_price(food_choice, drink_choice, dessert_choice)代码行时,Python会在当前脚本的作用域内查找food_choice、drink_choice、dessert_choice三个变量,由于这三个变量从未被赋值定义,直接触发NameError未定义报错。

现有代码说明

自定义模块(保存为module5.py)

#!/usr/bin/env python3

def show_food_menu():
    """This function only displays the menu"""
    print("Welcome to the General Fast Food")
    print("Press 1 to add pasta to your order $3")
    print("Press 2 to add salad to your order $6")
    print("Press 3 to add sandwich to your order $9")
    print("Press 0 if you don't want any food")

def show_drink_menu():
    """This function only displays the menu"""
    print("Welcome to the General Fast Food")
    print("Press 1 to add orange juice to your order $1")
    print("Press 2 to add water (free) to your order $2")
    print("Press 3 to add soda to your order $3")
    print("Press 0 if you don't want any drink")
    
def show_dessert_menu():
    """This function only displays the menu"""
    print("Welcome to the General Fast Food")
    print("Press 1 to add pie to your order $2")
    print("Press 2 to add cookies to your order $4")
    print("Press 3 to add fruit to your order $6")
    print("Press 0 if you don't want any dessert")
    
def get_user_choice():
    """This function reads the user choice and returns it
    Assume the user will always enter valid values"""
    choice = int(input("Choice: "))
    return choice

def get_total_price(food_choice, drink_choice, dessert_choice):
    """Function calculates the prices
    you need to pass, as a parameter, the choices that the user made
    when ordering their food, drink and dessert
    """
    price = food_choice * 3 + drink_choice * 1 + dessert_choice * 2
    return price

def print_total_price(total_price):
    """this fucntion just prints a friendly message with the
    final price for the customer
    """
    print("Thanks for buying our food. Your bill is $", total_price)

存在报错的运行脚本

#!/usr/bin/env python3
import module5
from module5 import *
module5.show_food_menu()
module5.show_drink_menu()
module5.show_dessert_menu()
module5.get_user_choice()
module5.get_total_price(food_choice, drink_choice, dessert_choice)
module5.print_total_price(total_price)

核心问题拆解

  • 未接收函数返回值:三次调用module5.get_user_choice()后,直接丢弃了返回的用户选择数值,没有把返回值赋值给food_choice、drink_choice、dessert_choice变量
  • 交互流程逻辑错误:一次性将三个品类的菜单全部打印,没有遵循“展示单个品类菜单→收集用户对应选择”的交互顺序
  • 导入方式冗余:同时使用import module5和from module5 import *两种导入方式,容易造成作用域变量混乱
  • 后续变量未定义:调用module5.get_total_price()计算总价后,没有把返回的总价结果赋值给total_price变量,传递给打印函数时也会触发未定义报错

修复后可正常运行的代码

将运行脚本替换为以下内容,与module5.py保存在同一目录下即可正常执行:

#!/usr/bin/env python3
import module5

# 依次展示菜单、收集用户选择并存入对应变量
module5.show_food_menu()
food_choice = module5.get_user_choice()

module5.show_drink_menu()
drink_choice = module5.get_user_choice()

module5.show_dessert_menu()
dessert_choice = module5.get_user_choice()

# 计算总价并存入变量
total_price = module5.get_total_price(food_choice, drink_choice, dessert_choice)
# 打印最终账单
module5.print_total_price(total_price)

内容的提问来源于stack exchange,提问作者IJeszI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.30 08:27:05