How to fix a Attribute Error for a 'NoneType' in python3?

How to fix a Attribute Error for a 'NoneType' in python3?



I'm trying to create a function that will merge two dictionaries through the use of Classes but I am unable to because I keep getting an AttributeError of 'NoneType.'However, when running the code individually without my entire code the function works, which confuses me.
My entire code is:


prices =
"apple": 1,
"beets": 1,
"carrots": 1

cookbook=

total=20


def handle_commands():
keep_going=True
while keep_going:
choices=input("$ ").strip().lower().split()
command=choices[0]
if command == "loadrecipefile":
recipe_file=choices[1]
loadrecipefile(recipe_file)
elif command == "printrecipes":
printrecipes()
elif command == "printiinventory":
printiinventory()
elif command == "printmoney":
printmoney()
elif command == "preparedish":
recipe_name=choices[1]
preparedish(recipe_name)
elif command == "buyingredient":
name=choices[1]
number=int(choices[2])
buyingredient(name, number)
elif command == "setprices":
apple_price=int(choices[1])
beets_price=int(choices[2])
carrots_price=int(choices[3])
setprices(apple_price, beets_price, carrots_price)
elif command == "mergerecipes":
r1=choices[1]
r2=choices[2]
mergerecipes(r1,r2)
elif command == "quit":
keep_going=False
break
else:
print("Sorry that is not an acceptable command")
return

def loadrecipefile (recipe_file):
infile=open(recipe_file)
Linelist=infile.readlines()
for line in Linelist:
wordList=line.split()
r1=Recipe(wordList[0],int(wordList[1]),int(wordList[2]),int(wordList[3]))
cookbook.addRecipe(r1)

def printrecipes():
for recipe in cookbook.allRecipes():
print(recipe.getName(),int(recipe.getApples()),int(recipe.getBeets()),int(recipe.getCarrots()))

def buyingredient(name, number:int):
global total
if number*prices[name] > total:
print("Not enough cash!")
if iinventory == 'apple':
iinventory.getApples()
elif iinventory == 'beets':
iinventory.getBeets()
elif iinventory == 'carrots':
iinventory.getCarrots()

iinventory[name] += number
total -= number*prices[name]

def printiinventory():
print(int(iinventory.getApples()), int(iinventory.getBeets()), int(iinventory.getCarrots()))

def printmoney():
print(total)

def CanPrepareDish(recipe_name):
recipe = cookbook[recipe_name]
for ingred in recipe:
if ingred not in iinventory:
return False
if iinventory[ingred] < recipe[ingred]:
return False
return True

def preparedish(recipe_name):
if not CanPrepareDish(recipe_name):
print("Not enough ingredients")
else:
recipe = cookbook[recipe_name]
for ingred in recipe:
iinventory[ingred] -= recipe[ingred]
if recipe_name in iinventory:
iinventory[recipe_name] +=1
else:
iinventory[recipe_name] = 1
print("Dish prepared")

#for recipe in cookbook.allRecipes():
#if recipe_name == recipe.getName():


def setprices(apple_price, beets_price, carrots_price):
for name,value in prices.items():
prices["apple"] = apple_price
prices["beets"] = beets_price
prices["carrots"] = carrots_price

def mergerecipes(r1,r2):
dish1 = cookbook.getRecipe(r1)
dish2 = cookbook.getRecipe(r2)
name = dish1.getName() + dish2.getName()
apple_num = dish1.getApples() + dish2.getApples()
beet_num = dish1.getBeets() + dish2.getBeets()
carrot_num = dish1.getCarrots() + dish2.getCarrots()
rnew=Recipe(name,apple_num,beet_num,carrot_num)

class Recipe:
def __init__(self,name,apple_num,beet_num,carrot_num):
self.name=str(name)
self.apple_num=int(apple_num)
self.beet_num=int(beet_num)
self.carrot_num=int(carrot_num)
def getName(self):
return self.name
def getApples(self):
return self.apple_num
def getBeets(self):
return self.beet_num
def getCarrots(self):
return self.carrot_num

class Iinventory:
def __init__(self):
self.apple_num=0
self.beets_num=0
self.carrots_num=0
def getApples(self):
return int(self.apple_num)
def getBeets(self):
return int(self.beets_num)
def getCarrots(self):
return int(self.carrots_num)

iinventory=Iinventory()

class Cookbook:
def __init__(self):
self.Cooklist=
def addRecipe(self,Recipe):
self.Cooklist.append(Recipe)
def getRecipe(self,recipe_name):
for recipe in self.Cooklist:
if recipe_name == recipe.getName():
return recipe
def allRecipes(self):
for Recipe in self.Cooklist:
return self.Cooklist

cookbook=Cookbook()

handle_commands()



But with only this portion of the code, the function mergerecipes works. I would like to know why and how to fix the error of fixing my code for that function to work in my entire code.



The snippet of my entire code:


cookbook=
class Recipe:
def __init__(self,name,apple_num,beet_num,carrot_num):
self.name=str(name)
self.apple_num=int(apple_num)
self.beet_num=int(beet_num)
self.carrot_num=int(carrot_num)
def getName(self):
return self.name
def getApples(self):
return self.apple_num
def getBeets(self):
return self.beet_num
def getCarrots(self):
return self.carrot_num

class Cookbook:
def __init__(self):
self.Cooklist=
def addRecipe(self,Recipe):
self.Cooklist.append(Recipe)
def getRecipe(self,recipe_name):
for recipe in self.Cooklist:
if recipe_name==recipe.getName():
return recipe
def allRecipes(self):
for Recipe in self.Cooklist:
return self.Cooklist

cookbook=Cookbook()

def mergerecipes(r1,r2):
dish1 = cookbook.getRecipe(r1)
dish2 = cookbook.getRecipe(r2)
name = dish1.getName() + dish2.getName()
apple_num = dish1.getApples() + dish2.getApples()
beet_num = dish1.getBeets() + dish2.getBeets()
carrot_num = dish1.getCarrots() + dish2.getCarrots()
rnew=Recipe(name,apple_num,beet_num,carrot_num)
cookbook.addRecipe(rnew)
print(rnew.getName(),int(rnew.getApples()),int(rnew.getBeets()),int(rnew.getCarrots()))

def loadrecipefile (recipe_file):
infile=open(recipe_file)
Linelist=infile.readlines()
for line in Linelist:
wordList=line.split()
r1=Recipe(wordList[0],int(wordList[1]),int(wordList[2]),int(wordList[3]))
cookbook.addRecipe(r1)

def printrecipes():
for recipe in cookbook.allRecipes():
print(recipe.getName(),int(recipe.getApples()),int(recipe.getBeets()),int(recipe.getCarrots()))



So when I load a textfile it inputs the "recipes" into the cookbook, which then you can see through printrecipes.



An example of an output would be:


loadrecipefile('recipes.txt')
printrecipes()
Dish1 2 4 1
Dish2 2 2 2
Dish3 1 2 4
Dish4 0 2 1
mergerecipes('Dish1','Dish3')
printrecipes()
Dish1 2 4 1
Dish2 2 2 2
Dish3 1 2 4
Dish4 0 2 1
Dish1Dish3 3 6 5



**IF CONFUSED: I tried running the merge recipes when running my entire code and I get this error:


$ loadrecipefile recipes.txt
$ printrecipes
Dish1 2 4 1
Dish2 2 2 2
Dish3 1 2 4
Dish4 0 2 1
$ mergerecipes Dish1 Dish2
name = dish1.getName() + dish2.getName()
AttributeError: 'NoneType' object has no attribute 'getName'



All help would be appreciated





Please show the full traceback. There's a lot of code here and the traceback would show us exactly where the problem is. Please see how to make an MCVE
– roganjosh
Aug 31 at 17:55






Give python-attribute-error-nonetype-object-has-no-attribute-something a read - it describes how to handle this kind of error.Use an IDE that allows step-wise code execution and debug your code.
– Patrick Artner
Aug 31 at 18:05




1 Answer
1



You're calling lower() on the input, so 'Dish1' becomes 'dish1', which is not found in the list of recipes, and so getRecipe() returns None.


lower()


'Dish1'


'dish1'


getRecipe()


None



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)