2014-09-21 64 views
-1

我是python編程的新手,目前我正在創建一個配方程序。我無法弄清楚如何擺脫這個錯誤。
錯誤說定義「食譜」,但我不知道該怎麼做。NameError:沒有定義全局名'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() 
+1

你在哪裏定義變量'Recipe'以在程序中調用爲'recipe = Recipe()'? – hjpotter92 2014-09-21 13:26:57

+1

這是你正在運行的代碼的全部?它看起來像「食譜」是一個尚未定義的類。 – 2014-09-21 13:27:12

+1

那麼,其中*是(類)'配方'的定義?另外(作爲一個樣式提示),您通常會調用實例方法'recipe.ingredientList(recipeFileName)',而不是調用類方法'Recipe.ingredientList(recipe,recipeFileName)'。 – 2014-09-21 13:28:00

回答

0

你怎麼看你的類叫IngredientRecipeFile?那些,像你定義的功能,被定義。不存在的東西(你沒有創建它們或導入它們)是未定義的。看起來你打算定義一個名爲Recipe的類。你應該這樣做。

+0

我確實通過'recipe = Recipe()'來定義類,就像我爲'Ingredient'和'RecipeFile'所做的那樣,但錯誤仍然存​​在! – 2014-09-21 13:52:19

+2

在你的代碼中沒有任何地方有一個聲明,說類的食譜.....當你做食譜=食譜()是定義一個名稱「食譜」,它是指一個食譜類的實例 - 但食譜類不是定義 - 因此錯誤。 – 2014-09-21 15:30:38

相關問題