我是python编程的新手,目前我正在创建一个配方程序。我无法弄清楚如何摆脱这个错误 该错误表示要定义" Recipe"但我不确定该怎么做。
Traceback (most recent call last):
File "E:/2.Recipe/new.py", line 164, in <module>
startup()
File "E://2.Recipe/new.py", line 159, in startup
recipe = Recipe()
NameError: global name 'Recipe' is not defined
非常感谢任何帮助!提前致谢! 我的代码是:
import os
mypath = "C:/Python33/Stuff/recipe"
class Ingredient():
name = ""
quantity = 0
unit = ""
def _init_(self):
self.name = ""
self.quantity = ""
self.unit = ""
def inputIngredientline(self):
self.name = input("The ingredient name: ")
if len(self.name)>0:
self.unit = input("the unit of measurement: ")
self.quantity = input("the quantity required: ")
def ingredientLine(self):
return self.quantity + "#" + self.unit + "#" + self.name + "#"
def readIngredientLine(self, ingredientLine):
hashTag1 = -1
hashTag2 = -1
hashTag3 = -1
charIndex = 0
noChar = len(self.ingredientLine())
if noChar > 0:
while charIndex0:
self.file = open(mypath + "/" + self.name + ".txt", "w")
self.person = input("The number of person this recipy is for: ")
self.file.writelines(self.person + "#" + self.name + "#" + "#")
self.ingredientEntry()
def readRecipeLine(self, recipeLine):
hashTag1 = -1
hashTag2 = -1
hashTag3 = -1
charIndex = 0
noChar = len(recipeLine())
if noChar > 0:
while charIndex0:
self.file.writelines(Ingredient.ingredientLine(self.ingredient))
nextEntry = True
else:self.file.close()
nextEntry = False
def ingredientList(self, fileName):
self.file = open(mypath + "/" + fileName, "r")
firstLine = True
for line in self.file:
if firstLine:
Recipe.readRecipeLine(self, line)
firstLine = False
else:
if len(line)>0:
ingredient = Ingredient()
Ingredient.readIngredientLine(ingredient, line)
self.ingredients.append(ingredient)
self.file.close()
def outputRecipeForX(self, xperson):
print("")
print("")
print("For " + self.name + " for " + str(xperson) + " persons you will need: ")
ratio = (int(xperson)/int(self.person))
for ingredient in self.ingredients:
if len(ingredient.quantity)>0:
quantity = int(ingredient.quantity)
print (" --> " + ingredient.name + ": " + str(quantity) + " " + ingredient.unit)
class RecipeFile():
filename = ""
fileIndex = 0
def _init_(self):
self.filename = ""
self.fileIndex = 0
def toString(self):
return str(self.fileIndex) + " " + self.filename
def chooseRecipe():
recipeFile = RecipeFile()
f = [RecipeFile()]
i = 0
default = 0
noChoice = True
for (dirpath, dirname, filenames) in os.walk(mypath):
for filename in filenames:
recipeFile = RecipeFile()
recipeFile.filename = filename
recipeFile.fileIndex = i
if i==0:
f = [recipeFile]
else:
f.append(recipeFile)
i = i + 1
for oneFile in f:
print (RecipeFile.toString(oneFile))
while noChoice:
choice = input("Enter the number of the recipe you wish to view: ").lower()
if choice == "":
noChoice = True
elif int(choice) < (len(f)):
recipeFile = f[int(choice)]
noChoice = False
else:
error = "Please respond with a number between 0 and %i." % ((len(f)-1))
print (error)
return (recipeFile.filename)
def chooseNbPersons():
people = 0
default = 2
noChoice = True
while noChoice:
choice = input("Enter the number of persons for the recipe (default 2): ")
if choice == "":
people = int(default)
noChoice = False
elif choice.isdigit():
people = int(choice)
noChoice = False
else:
error = "Please respond with a number."
print (error)
noChoice = True
return people
def chooseMode():
valid = {"New":True, "N":True, "new":True, "n":True, "View":False, "V":False, "view":False, "v":False}
default = "View"
while True:
choice = input("Do you want to enter a new Recipe or view an existing one? (new/view)").lower()
if choice == "":
return valid[default]
elif choice in valid:
return valid[choice]
else:
print("Please respond with 'new' or 'view' (or 'n' or 'v').")
def startup():
recipeFileName = ""
if chooseMode():
recipe = Recipe
recipe.createRecipe(recipe)
else:
recipe = Recipe()
recipeFileName = chooseRecipe()
recipe.ingredientList(recipeFileName)
recipe.outputRecipeForX(chooseNbPersons)
startup()
答案 0 :(得分:0)
您会看到如何使用名为Ingredient
和RecipeFile
的课程?这些与您定义的函数一样,是定义的。不存在的东西(你没有创建它们或导入它们)是不确定的。看起来您打算定义一个名为Recipe
的类。你应该这样做。