-1

嗨,我正在开发一个程序,将一些文件存储到文本文档中,必要时可以重新加载。下面是代码的开头,但是当我认为我已将其定义为文本文件的名称时,我收到一条回溯错误,指出“recipe_title 未定义”。请帮我看看我做错了什么。

import sys

opt=0

def choice1():
    print("WORKED")
def choice2():
    Recipe_Name = input("Please enter a recipe name: ")
    Recipe_List = open(recipe_title.txt,"w")
    Recipe_List.write(recipe_title+"\n")

def ingredient_input_loop(recipe_title, ):
        Recipefile = open(recipe_title,"w")
        if(ingredient== "end" or "End" or "END" or "EnD" or "eNd" or "enD" or "ENd" or "eND"):
            Recipe.write(recipe_title)
4

1 回答 1

1

recipe_title.txt 是您的文件名,而不是变量。因此,您应该添加引号

Recipe_List = open('recipe_title.txt',"w")

或者如果 recipe_title 真的是一个变量:

Recipe_List = open('{}.txt'.format(recipe_title),"w") # now you can open brocolli.txt for example

关于您的代码的一般反馈:

  • 变量名称不应包含大写字符。这应该只用于类名。
  • 检查是否可以将“end”的所有组合写入if ingredient.lower() == "end":
于 2015-03-17T14:23:25.747 回答