为什么Python类中有错误?

时间:2015-04-01 22:32:41

标签: python csv

我正在做一个分配,我遇到了一些问题,试着让这个代码在Classes上工作,它出现错误,它链接到一个CSV文件:

#!/usr/bin/python3

from rect import Rectangle
##---------------------
## Counter class
##---------------------
class Counter():
   def __init__(self):
      self.tCount  = 0
      self.gasCount = {}
      self.qsum = 0
      self.cCount = 0
      self.clines = 0
      self.ageCount = 0
      self.tAge = 0
      self.lensum = 0

   def add(self, rectangle):
      if rectangle.getQuant < 408:
         self.qusm += self.quant

   def q3(self):
      return ("The sum of vaiues in field [quant[ less than 408: %d" %
              (self.tCount))

   def reset(self):
      self.tCount = 0

##-------------------
## FileReader class
##--------------------
class FileReader():

   def __init__(self, filename, counter):
      self.counter = counter
      self.filename = filename

   def run(self):
      theFile = open( self.filename,'r')
      firstLine = True
         # looking at each line in the file in turn
      for line in theFile:
         if firstLine: # I do not want to read the field names
            firstLine = False   # so I skip them.
            continue

         fields = line.split(',')
             # first I extract the values from the line
         correct = fields[0].strip()
         gas = fields[1].strip()
         quant = int(fields[2].strip())
         code = fields[3].strip()
         age = int(fields[4].strip())
         leng = float(fields[5].strip())
             # then I pass a complete Rectangle to the counter object 
         self.counter.add(Rectangle(self,correct,gas,quant,age,leng))

      theFile.close() 

##--------------------
## Demonstrator class
##   this will later be replaced by our GUI class
##--------------------
class Demonstrator():

   def __init__(self,filename):
      self.counter = Counter()
      self.reader = FileReader(filename, self.counter)
      self.reader.run()

   def displayq3(self):
      print(self.counter.q3())


   def newFile(self, filename):
      self.counter.reset() # throw away any old results
      self.reader = FileReader( filename, self.counter)
      self.reader.run()

#----------------------------
# now show how we use these classes
# it is increadibly simple
#----------------------------

print("\nFirst calculating with the file 3215402a.csv")
d = Demonstrator("3215402a.csv")
d.displayq3()

上面的代码链接到下面的代码

class Rectangle():

    def __init__(self, correct,gas,quant,code,age,leng):
        self.correct = correct
        self.gas = gas
        self.quant = quant
        self.code = code
        self.age = age
        self.leng = leng

    def getCorrect(self):
        return self.correct

    def getGas(self):
        return self.gas

    def getQuant(self):
        return self.quant

    def getCode(self):
        return self.code

    def getAge(self):
        return self.age

    def getLeng(self):
        return self.leng

我得到的错误是:

Traceback (most recent call last):
  File "D:\Users\Timothy Lam\Documents\BSc (Hons) Information Technology (with Placement) (4350)\Ebad Banissi\Software Development for Business 2\Assignments\3215402\3215402 Stage 2 Class.py", line 86, in <module>
    d = Demonstrator("3215402a.csv")
  File "D:\Users\Timothy Lam\Documents\BSc (Hons) Information Technology (with Placement) (4350)\Ebad Banissi\Software Development for Business 2\Assignments\3215402\3215402 Stage 2 Class.py", line 69, in __init__
    self.reader.run()
  File "D:\Users\Timothy Lam\Documents\BSc (Hons) Information Technology (with Placement) (4350)\Ebad Banissi\Software Development for Business 2\Assignments\3215402\3215402 Stage 2 Class.py", line 56, in run
    self.counter.add(Rectangle(correct,gas,quant,age,leng))
TypeError: object() takes no parameters

我有一个新错误:

Traceback (most recent call last):
  File "D:\Users\Timothy Lam\Documents\BSc (Hons) Information Technology (with Placement) (4350)\Ebad Banissi\Software Development for Business 2\Assignments\3215402\3215402 Stage 2 Class.py", line 86, in <module>
    d = Demonstrator("3215402a.csv")
  File "D:\Users\Timothy Lam\Documents\BSc (Hons) Information Technology (with Placement) (4350)\Ebad Banissi\Software Development for Business 2\Assignments\3215402\3215402 Stage 2 Class.py", line 69, in __init__
    self.reader.run()
  File "D:\Users\Timothy Lam\Documents\BSc (Hons) Information Technology (with Placement) (4350)\Ebad Banissi\Software Development for Business 2\Assignments\3215402\3215402 Stage 2 Class.py", line 56, in run
    self.counter.add(Rectangle(correct,gas,quant,age,leng))
TypeError: __init__() missing 1 required positional argument: 'leng'

我现在又改变了另一个新错误:

Traceback (most recent call last):
  File "D:\Users\Timothy Lam\Documents\BSc (Hons) Information Technology (with Placement) (4350)\Ebad Banissi\Software Development for Business 2\Assignments\3215402\3215402 Stage 2 Class.py", line 86, in <module>
    d = Demonstrator("3215402a.csv")
  File "D:\Users\Timothy Lam\Documents\BSc (Hons) Information Technology (with Placement) (4350)\Ebad Banissi\Software Development for Business 2\Assignments\3215402\3215402 Stage 2 Class.py", line 69, in __init__
    self.reader.run()
  File "D:\Users\Timothy Lam\Documents\BSc (Hons) Information Technology (with Placement) (4350)\Ebad Banissi\Software Development for Business 2\Assignments\3215402\3215402 Stage 2 Class.py", line 56, in run
    self.counter.add(Rectangle(self,correct,gas,quant,age,leng))
  File "D:\Users\Timothy Lam\Documents\BSc (Hons) Information Technology (with Placement) (4350)\Ebad Banissi\Software Development for Business 2\Assignments\3215402\3215402 Stage 2 Class.py", line 19, in add
    if rectangle.getQuant < 408:
TypeError: unorderable types: method() < int()

有没有解决方法如何解决这个问题。 感谢

2 个答案:

答案 0 :(得分:2)

观看"dunder"

替换:

def _init_(self, correct,gas,quant,code,age,leng):

使用:

def __init__(self, correct, gas, quant, code, age, leng):

答案 1 :(得分:1)

你在说这个:

Rectangle(correct,gas,quant,age,leng)

但是根据你的Rectangle类,它应该是这样的:

Rectangle(self, correct,gas,quant,code,age,leng)

所以基本上你忘记了那里的code参数。