免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2353 | 回复: 4
打印 上一主题 下一主题

麻烦大家帮我看看这个程序为什么总是报错呀 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-14 16:51 |只看该作者 |倒序浏览
1 class Food:
  2         def __init__(self,name):
  3                 self.name=name
  4
  5 class Employee:
  6         def takeOrder(self,foodName):
  7                 return Food(foodName)
  8
  9 class Customer:
10         def __init__(self):
11                 print 'in customer init'
12                 self.food=None
13         def placeOrder(self,foodName,empoyee):
14                 self.food=employee.takeOrder(foodName)
15         def printFood(self):
16                 print self.food.name
17
18 class Lunch:
19         def __Init__(self):
20                 self.custom=Customer()
21                 self.empl=Employee()
22
23         def order(self,foodName):
24                 self.custom.placeOrder(foodName,self.empl)
25         def result(self):
26                 self.cust.printFood()
27
28 if __name__=='__main__':
29         x=Lunch()
30         x.order('burritors')
31         x.result()
32         x.order('pizza')
33         x.result()
34
~


python lunch.py
Traceback (most recent call last):
  File "lunch.py", line 30, in ?
    x.order('burritors')
  File "lunch.py", line 24, in order
    self.custom.placeOrder(foodName,self.empl)
AttributeError: Lunch instance has no attribute 'custom'

麻烦大家帮我看看,为什么总报错呀,谢谢了。

论坛徽章:
0
2 [报告]
发表于 2009-01-14 17:09 |只看该作者
对于楼主这样的提问,建议:
第一。不要帖行号上来。这样别人即使拷贝你的程序,也要一行行去掉。
第二。你自己检查下程序,别从网上随便拷下来就保存来发。注意拼写错误

你的错误是下面几个
1. 第19行def __Init__(self):单词错误,应该是__init__,注意是小写
2. 第26行self.cust.printFood(),应该是self.custom.printFood()
3. 第13行def placeOrder(self,foodName,empoyee):,应该是def placeOrder(self,foodName,employee):


正确的代码如下:
class Food:
    def __init__(self,name):
        self.name=name

class Employee:
    def takeOrder(self,foodName):
        return Food(foodName)

class Customer:
    def __init__(self):
        print 'in customer init'
        self.food=None
    def placeOrder(self,foodName,employee):
        self.food=employee.takeOrder(foodName)
    def printFood(self):
        print self.food.name

class Lunch:
    def __init__(self):
        self.custom=Customer()
        self.empl=Employee()

    def order(self,foodName):
        self.custom.placeOrder(foodName,self.empl)
    def result(self):
        self.custom.printFood()

if __name__=='__main__':
    x=Lunch()
    x.order('burritors')
    x.result()
    x.order('pizza')
    x.result()

论坛徽章:
0
3 [报告]
发表于 2009-01-14 17:13 |只看该作者
呵呵,好版主。

论坛徽章:
0
4 [报告]
发表于 2009-01-15 08:38 |只看该作者
谢谢版主,真是好人呀!!!!!!!

论坛徽章:
0
5 [报告]
发表于 2009-01-15 10:02 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP