Recipe Finder,Python 3.保存配方时的定义错误

时间:2015-03-11 13:29:44

标签: python

以下是我遇到的问题的代码。在保存新配方时,会出现错误,指出“菜肴”未定义。 'dish'是保存食谱名称的变量。要进入此阶段,必须已输入名称,因此我无法确定错误发生的原因。我会非常感谢任何评论和提示,因为我已经触及了隐喻的“砖墙”。

导入sys

recipes = []
ingredients = []
quantities = []

menu = "What would you like to do? \n 1) Make a recipe \n 2) Load a recipe \n 3) Exit \n\
"

def save_recipe(recipe, file_name):
    file = open(file_name, "w")
    file.write(recipe)
    file.close()

def decision_functions():
    interface_choice = input(menu)
    if (interface_choice == "1" or "Make a recipe"):
        dish = input("What do you wish to make?: ")
        ingredients_loop()
    elif (interface_choice == "2" or "Load a recipe"):
        decision = input("What recipe do you wish to load?: ")
        decision += ".txt"
        file = open(decision, "r")
        recipe = (file.read())
        serves = int(input("How many do you wish to serve?: "))
        recipe = list(recipe)


    elif (interface_choice == "3" or "Exit"):
        print("Goodbye!")
        sys.exit

    else:
        print("I don't understand. . . Please select one of the above numbered options. \n\
")
        decision_functions

def ingredients_loop():
    ingredient = input("Name an ingredient in the recipe. Type END when done: ")
    if (ingredient == "end"):
        def confirm_decision():
            print(ingredients)
            print(quantities)
            confirmation = input("Is this all?: ")
            if (confirmation == "yes"):
                recipe = [ingredients, "\n\
"                       , quantities]
                file_name = dish.replace(' ', '') + ".txt"
                writerecipe(str(recipe), file_name)
                print("Your recipe has been saved")
                decision_functions()
            elif (confirmation == "no"):
                print("Preparing to allow more ingredients. . .")
                ingredients_loop()
            else:
                print("I do not understand . . .")
                confirm_decision()
        confirm_decision() 
    else:
        ingredient += ":"
        ingredients.append (ingredient)
        amount = int(input("How much of this ingredeint is required to serve one?: "))
        units = (input("What unit is this measurement in?: "))
        amts = amount, units
        quantities.append (amts)
    ingredients_loop()


decision_functions()

1 个答案:

答案 0 :(得分:0)

您正在ingredients_loop内使用菜肴,因为您已在decision_functions

内声明,因此无法访问

你可以做的是将菜肴作为全球菜肴或将菜肴传递给ingredients_loop

recipes = []
ingredients = []
quantities = []
dish =""

menu = "What would you like to do? \n 1) Make a recipe \n 2) Load a recipe \n 3) Exit \n\
"

dish指定为全局