2011-08-15 18 views
4

我正在爲CS-600開放麻省理工學院開放課件,我無法弄清楚爲什麼最後的打印語句不打印任何東西。這是我寫的代碼:爲什麼我的主要方法底部的print語句不能打印任何內容?

#!/usr/bin/env python 
# encoding: utf-8 
# 6.00 Problem Set 9 
# 
# Name: 
# Collaborators: 
# Time: 

from string import * 

class Shape(object): 
    def area(self): 
     raise AttributeException("Subclasses should override this method.") 

class Square(Shape): 
    def __init__(self, h): 
     """ 
     h: length of side of the square 
     """ 
     self.side = float(h) 
    def area(self): 
     """ 
     Returns area of the square 
     """ 
     return self.side**2 
    def __str__(self): 
     return 'Square with side ' + str(self.side) 
    def __eq__(self, other): 
     """ 
     Two squares are equal if they have the same dimension. 
     other: object to check for equality 
     """ 
     return type(other) == Square and self.side == other.side 

class Circle(Shape): 
    def __init__(self, radius): 
     """ 
     radius: radius of the circle 
     """ 
     self.radius = float(radius) 
    def area(self): 
     """ 
     Returns approximate area of the circle 
     """ 
     return 3.14159*(self.radius**2) 
    def __str__(self): 
     return 'Circle with radius ' + str(self.radius) 
    def __eq__(self, other): 
     """ 
     Two circles are equal if they have the same radius. 
     other: object to check for equality 
     """ 
     return type(other) == Circle and self.radius == other.radius 

# 
# Problem 1: Create the Triangle class 
# 
## TO DO: Implement the `Triangle` class, which also extends `Shape`. 

class Triangle(Shape): 
    def __init__(self, base, height): 
     self.base = float(base) 
     self.height = float(height) 
    def area(self): 
     return self.base*self.height/2 
    def __str__(self): 
     return 'Triangle with base ' + str(self.base) + 'and height ' + str(self.height) 
    def __eq__(self, other): 
     return type(other) == Triangle and self.base == other.base and self.height == other.height 
# 
# Problem 2: Create the ShapeSet class 
# 
## TO DO: Fill in the following code skeleton according to the 
## specifications. 

class ShapeSet(object): 
    def __init__(self): 
     """ 
     Initialize any needed variables 
     """ 
     self.allCircles = [] 
     self.allSquares = [] 
     self.allTriangles = [] 
     self.allShapes = self.allCircles + self.allSquares + self.allTriangles 
     self.place = None 
    def addShape(self, sh): 
     """ 
     Add shape sh to the set; no two shapes in the set may be 
     identical 
     sh: shape to be added 
     """ 
     if not isinstance(sh, Shape): raise TypeError('not a shape') 
     if isinstance(sh, Square): 
      for sq in self.allSquares: 
       if sh == sq: 
        raise ValueError('shape already in the set') 
      self.allSquares.append(sh) 
     if isinstance(sh, Triangle): 
      for tri in self.allTriangles: 
       if sh == tri: 
        raise ValueError('shape already in the set') 
      self.allTriangles.append(sh) 
     if isinstance(sh, Circle): 
      for circ in self.allCircles: 
       if sh == circ: 
        raise ValueError('shape already in the set') 
      self.allCircles.append(sh) 
    def __iter__(self): 
     """ 
     Return an iterator that allows you to iterate over the set of 
     shapes, one shape at a time 
     """ 
     self.place = 0 
     return self 
    def next(self): 
     if self.place >= len(self.allShapes): 
      raise StopIteration 
     self.place += 1 
     return self.allShapes[self.place - 1] 

    def __str__(self): 
     """ 
     Return the string representation for a set, which consists of 
     the string representation of each shape, categorized by type 
     (circles, then squares, then triangles) 
     """ 
     shapeList = "" 
     for item in self.allShapes: 
      shapeList += item.get__str__ + "br/" 
     return shapeList 

# 
# Problem 3: Find the largest shapes in a ShapeSet 
# 
def findLargest(shapes): 
    """ 
    Returns a tuple containing the elements of ShapeSet with the 
     largest area. 
    shapes: ShapeSet 
    """ 
    ## TO DO 

# 
# Problem 4: Read shapes from a file into a ShapeSet 
# 
def readShapesFromFile(filename): 
    """ 
    Retrieves shape information from the given file. 
    Creates and returns a ShapeSet with the shapes found. 
    filename: string 
    """ 
    ## TO DO 

def main(): 
    sq1 = Square(4.0) 
    sq2 = Square(5.0) 
    sq3 = Square(3.0) 
    circ1 = Circle(3.0) 
    circ2 = Circle(3.2) 
    tri1 = Triangle(3.0, 4.0) 
    tri2 = Triangle(4.0, 3.0) 
    tri3 = Triangle(1.0, 1.0) 
    thisSet = ShapeSet() 
    thisSet.addShape(sq1) 
    thisSet.addShape(sq2) 
    thisSet.addShape(sq3) 
    thisSet.addShape(circ1) 
    thisSet.addShape(circ2) 
    thisSet.addShape(tri1) 
    thisSet.addShape(tri2) 
    thisSet.addShape(tri3) 
    print thisSet 



if __name__ == '__main__': 
    main() 

回答

1

此行使得allShapes空列表:

self.allShapes = self.allCircles + self.allSquares + self.allTriangles 

如果修改allCircles,不影響allShapes。我會親自消除allShapes,並在STR方法,在最後可能的第二添加它們:

for item in self.allCircles + self.allSquares + self.allTriangles: 
6

這條線:

self.allShapes = self.allCircles + self.allSquares + self.allTriangles 

沒有做什麼,你認爲它。它將allShapes設置爲空列表,然後在稍後添加形狀時,沒有更新allShapes

然後你的__str__功能只是循環過allShapes,它仍然是空的,所以你的__str__返回一個空字符串。

0

如果這是你實際的代碼,那麼它一定是因爲

item.get__str__ 

應引發異常。

編輯:正如其他人所指出的,這不是實際的問題,但我留下這裏作爲進一步進步的暗示。請注意,您可能打算直接撥打x.__str__(),這被認爲是不好的風格(「unpythonic」)。改爲撥打str(x),即使在執行__str__

0

分配allShapes是的self.allCircles + self.allSquares + self.allTriangles的在你的init方法中開始(當其他列表爲空時)。

它的價值從未改變,所以它仍然是空的。

你需要這個在addShape:

self.allShapes.append(sh) 
1

的問題是在這裏:

self.allShapes = self.allCircles + self.allSquares + self.allTriangles 

當您連接喜歡這個名單,結果是組件列表的副本。因此,稍後更改這些列表時,連接列表不會更改。在這種情況下,self.allCircles等都是空的。所以self.allShapes也是一個空列表; ShapeSet.__str__中的for循環不會將任何內容追加到ShapeList,所以結果是一個空字符串。

一個簡單的方法來解決這一問題將是使allShapes一個方法你打電話,並返回self.allCircles一個新的串聯......每次這就是所謂的時間等。這樣,allShapes始終是最新的。

相關問題