0

我是这个网站的新手,也是编程的新手,我正在尝试使用一本针对 python 3.1 初学者的书来学习 python。我遇到了一个无论我尝试什么都行不通的例子,我在写作中搜索了大约 10 次错误,但它似乎与我在书中看到的完全一致。这是一个例子:

冰箱课

#!/usr/bin/env
class Fridge:
    """methods:
    has(food_name [, quantity])-chk if string is in the fridge
    has_various(foods)-chk if enough food is in the fridge
    add_one(food_name) -adds 1 food
    add_many(food_dict)- adds dict to fridge
    get_one(food_name)- take out 1 food
    get_many(food_dict) - a dict out of fridge
    get_ingred(food)- if passed an obj get the list of __ingredients__
    """
    def __init__(self, items={}) :
        if type(items) != type({}):
            raise typeError("Fridge req a dict but was given %s" %  type(items))
        self.items=items
        return
    def __add_multi(self, food_name, quantity):
        if (not food_name in self.items):
            self.items[food_name]=0
        self.items[food_name]=self.items[food_name]+quantity
    def add_one(self, food_name):
        if type(food_name) != type(""):
            raise TypeError ("add_one requires a string givem a %s " % type(food_name))
        else:
            self.__add_multi(food_name, 1)
        return True
    def add_many(self, food_dict):
        if type(food_dict) != type({}):
            raise TypeError ("add_many requires a dict, got a %s" % type(food_dict))
        for item in food_dict.keys() :
            self.__add_multi(item, food_dict[item])
        return
    def has(self, food_name, quantity=1):
        return self.has_varoius({food_name:quantity})
    def has_various(self, foods):
        try:
            for food in foods.keys():
                if self.items[food] < foods[food]:
                    return False
            return True
        except KeyError:
            return Fasle
    def __get_multi(self, food_name, quantity):
        try:
            if (self.items[food_name] is None) :
                return False
            if (quantity > self.items[food_name]):
                return False
            self.items[food_name] = self.items[food_name] - quantity
        except KeyError:
            return False
        return quantity
    def get_one(self, food_name):
        if type(food_name) !=type(""):
            raise TypeError("get_one requires a string and was given a %s" % type(food_name))
        else:
            result=self.__get_multi(food_name, 1)
            return result
    def get_many(self, food_dict):
        if self.has_various(food_dict):
            foods_removed={}
            for item in food_dict.keys():
                foods_removed[item]=self.__get_multi(item, food_dict[item])
            return foods_removed
    def get_ingredients(self, food):
        try:
            ingredients=self.get_many(food.__ingredients())
        except AttributeError:
            return False
        if ingredients!=False:
            return ingredients

煎蛋卷类

 #!/usr/bin/env python3.3
class Omelet:

    def __init__(self, kind="cheese"):
        self.set_kind(kind)
        return

    def __ingredients__(self):
        return self.needed_ingredients

    def get_kind(self):
        return self.kind

    def set_kind(self, kind):
        possible_ingredients=self.__known_kinds(kind)
        if possible_ingredients == False :
            return False
        else:
            self.kind=kind
            self.needed_ingredients= possible_ingredients

    def set_new_kind(self, name, ingredients):
        self.kind=name
        self.needed_ingredients= ingredients
        return

    def __known_kinds(self, kind):
        if kind == "cheese":
            return {"eggs":2, "milk":1, "cheese":1}
        elif kind == "mushroom":
            return {"eggs":2, "milk":1, "cheese":1, "mushroom":2}
        elif kind == "onion":
            return {"eggs":2, "milk":1, "cheese":1, "onion":1}
        else:
            return False

    def get_ingredients(self, fridge):
        self.from_fridge= fridge.get_ingredients(self)

    def mix(self):
        for ingredient in self.from_fridge.keys():
            print("mixing %d %s for the %s omelet" % (self.from_fridge["ingredient"], ingredient, self.kind))
        self.mixed=True

    def make(self):
        if self.mixed == True:
            print("Cooking the %s omelet!" % self.kind)
            self.cooked = True 

这就是我调用类的方式以及使用这些类的方法以及我得到的错误

>>> exec(open("/home/knoppix/test/fridge.py").read())
>>> exec(open("/home/knoppix/test/omelet.py").read())
>>> o=Omelet("cheese")
>>> f=Fridge({"cheese":5, "milk":4, "eggs":12})
>>> o.get_ingredients(f)
>>> o.mix()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 41, in mix
AttributeError: 'bool' object has no attribute 'keys'

如果代码中有打字错误,请原谅,这正是我在书中找到的!

4

2 回答 2

2
def get_ingredients(self, fridge):
    self.from_fridge= fridge.get_ingredients(self)

在这个函数中,你fridge.get_ingredients()可能会返回False.

所以self.from_fridgeBoolean没有keys()方法的价值。

您可能需要添加适当的签入mix()方法。

于 2013-08-01T06:24:51.690 回答
1

函数“__known_kinds(kind)”最好返回 {} 以保持一致,而不是不同的对象类型,尽管“None”比“False”好。

    if kind == "cheese":
        return {"eggs":2, "milk":1, "cheese":1}
    elif kind == "mushroom":
        return {"eggs":2, "milk":1, "cheese":1, "mushroom":2}
    elif kind == "onion":
        return {"eggs":2, "milk":1, "cheese":1, "onion":1}
    else:
        return {}

然后你只需要在 mix() 方法中处理字典类型。else 也可以从 mix() 中删除,因为如果 dict == {} 将引发异常。

def mix(self):
    if self.from_fridge == {}:
        raise IndexError("self.from_fridge returns Nothing")

    for ingredient in self.from_fridge.keys():     

……

于 2015-06-18T13:05:54.257 回答