Answers to Exercises
print “Failed to set the ingredients for the western” else:
print “Succeeded in getting the ingredients for the western.”
Exercise 2 solution
At the end of the Fridge module, insert the following code. Note the comment about changing the add_many function to return True. If you don’t do that, add_many will return None, and this test will always fail!
if __name__ == ‘__main__’:
f = Fridge({“eggs”:10, “soda”:9, “nutella”:2}) if f.has(“eggs”) != True:
print “Failed test f.has(‘eggs’)” else:
print “Passed test f.has(‘eggs’)” if f.has(“eggs”, 5) != True:
print “Failed test f.has(‘eggs’, 5)” else:
print “Passed test f.has(‘eggs’, 5)”
if f.has_various({“eggs”:4, “soda”:2, “nutella”:1}) != True:
print ‘Failed test f.has_various({“eggs”:4, “soda”:2, “nutella”1})’ else:
print ‘Passed test f.has_various({“eggs”:4, “soda”:2, “nutella”1})’
#Check to see that when we add items, that the number of items in the fridge
#is increased!
item_count = f.items[“eggs”] if f.add_one(“eggs”) != True:
print ‘Failed test f.add_one(“eggs”)’ else:
print ‘Passed test f.add_one(“eggs”)’ if f.items[“eggs”] != (item_count + 1):
print ‘Failed f.add_one() did not add one’ else:
print ‘Passed f.add_one() added one’ item_count = {}
item_count[“eggs”] = f.items[“eggs”] item_count[“soda”] = f.items[“soda”]
# Note that the following means you have to change add_many to return True! if f.add_many({“eggs”:3,”soda”:3}) != True:
print ‘Failed test f.add_many({“eggs”:3,”soda”:3})’ else:
print ‘Passed test f.add_many({“eggs”:3,”soda”:3})’ if f.items[“eggs”] != (item_count[“eggs”] + 3):
print “Failed f.add_many did not add eggs” else:
print “Passed f.add_many added eggs”
if f.items[“soda”] != (item_count[“soda”] + 3): print “Failed f.add_many did not add soda”
else:
print “Passed f.add_many added soda”
item_count = f.items[“eggs”] if f.get_one(“eggs”) != True: