0
class Packagings:
    def _init_(self):
        self.length,self.deckle,self.tmp,self.flute,self.gsm,self.t_weight,self.weight

    def read_values(self):
        print """Select Type Of Paper
               1.3 Ply
               2.5 Ply
               3.7 Ply
               """
        self.type=input("Enter the type:")
        self.l=input("Enter Length:")
        self.b=input("Enter Breadth:")
        self.h=input("Enter Height:")
        self.flap=input("Enter Flap:")


    def ply_read_values(self):
        for i in range(0,3):
            self.flute[i]=input("Enter flute:")
            self.gsm[i]=input("Enter Gsm:")
            self.weight[i]=self.tmp*(flute[i]*gsm[i])
            self.t_weight=self.t_weight+self.weight[i]

    def do_calc(self):
        self.length=(2*self.l+2*self.b+self.flap)/1000
        self.deckle=(float(self.h+self.b))/1000
        self.tmp=self.length*self.deckle


    def print_value(self):
        print self.length
        print self.deckle
        print self.t_weight

#Main Function
obj=Packagings()
obj.read_values()
obj.do_calc()
obj.ply_read_values()
obj.print_value()

尝试运行程序时出现以下错误:

Traceback (most recent call last):
  File "C:/Python27/packagings.py", line 41, in <module>
    obj.ply_read_values()
  File "C:/Python27/packagings.py", line 21, in ply_read_values
    self.flute[i]=input("Enter flute:")
AttributeError: Packagings instance has no attribute 'flute' 

在 python 程序中使用列表有什么特定的方法吗?我认为错误是由于我以错误的方式使用列表。你能通过代码告诉我哪里出错了吗?

4

2 回答 2

5

你想用这条线做什么?:

class Packagings:
    def _init_(self):      # first, I suppose you wanted to write __init__ as @larsmans noticed in comment
        self.length,self.deckle,self.tmp,self.flute,self.gsm,self.t_weight,self.weight

如果你不影响他们做某事,那些成员就永远不会存在。

试试这个:

class Packagings:
    def __init__(self):
        self.length = 0
        self.deckle = 0.0
        self.tmp = 0.0
        self.flute = []
        self.gsm = []
        self.t_weight = 0
        self.weight = 0
于 2012-06-21T11:55:07.453 回答
0

正如拉尔斯曼斯所说,你必须更换

def _init_(self):

经过

def __init__(self):

在您的代码中,永远不会调用 init,因此永远不会创建您的数组。

然后,查看 Cédric Julien 的帖子,了解如何正确创建您的属性

于 2012-06-21T12:17:49.033 回答