0

I am having an issue with a project due. I am missing something? can you please help?
here are the instructions and what i have so far.
The final project will consist of three files called, Shape.py, Circle.py, and Square.py. I’ve written a test file you can use to see if your code is working correctly.

Create the files Shape.py, Circle.py, and Square.py. Each file will contain one class.

Shape.py will contain the Shape class. This will be the base class for the Circle and Square classes.

The Shape class contains a class attribute called shape_name, x, and y. Create a constructor method (initialization method). This method creates and initializes x and y. x and y represent a point where the shape is. The shape_name attribute is a name that you create when you instantiate a Circle or a Square.

Create the __str__ method in the Shape class that returns a string that contains the name of the shape and the x and y values.

Create a move method in the Shape class that accepts two values. These values are added to x and y to simulate moving the shape.

The Circle class is based on the Shape class. Create a constructor that accepts a name, x and y as well as a radius. Also create a __str__method and a method called area. The area method returns radius * radius * 3.14159.

The Square class is based on the Shape class. Create a constructor that accepts a name, x and y as well as a side. Also create a __str__method and a method called area. The area method returns side * side.

The Circle and Square classes init method must call the Shape init method.

The Circle and Square classes str method calls Shape’s str method.

Here’s some code to test your classes:

# main

from Circle import Circle

from Square import Square

circle = Circle("aCircle", 2, 2, 3)

square = Square("aSquare", 0, 0, 4)

print circle

print square

print

print circle.area()

print square.area()

print

circle.move(-1, -2)

print circle

print

square.move(2, 3)

print square

Here is my base file Shape.py
# Shape Class
import Square, Circle

class Shape(object):
def __init__(self, x, y):
self.pos = x, y

def move(self, Circle, Square):
x, y = self.pos
self.pos = x + shape_name, y + shape_name


# main

from Circle import Circle

from Square import Square

circle = Circle("aCircle", 2, 2, 3)

square = Square("aSquare", 0, 0, 4)

print circle

print square

print

print circle.area()

print square.area()

print

circle.move(-1, -2)

print circle

print

square.move(2, 3)

print square

HERE IS WHAT I HAVE FOR CIRCLE.PY
#circle class

class Circle(Shape):
"""Circle Class"""
Circle = ["Circle"]
def __init__(self, shape, x, y, radius):
super(Circle, self).__init__(Circle)
self.radius = radius
def __str__(self):
rep = self.Circle
return rep

AND SQUARE.PY
# Square Class

class Square(object):
"""Square Class"""
Square = ["Square"]
def __init__(self, x, y, shape_name, side):
super(Square, self).__init__(Square)
self.side = side

def __str__(self):
rep = self.name
return rep


the following error is returned:
Traceback (most recent call last):
File "C:\Users\Guess Who\Desktop\Programing Class\Shape.py", line 25, in -toplevel-
print circle
TypeError: __str__ returned non-string (type list)

thank you

2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by jlm699
0

Use code tags when posting code in this forum, or else nobody will be willing to help you:
[code=python] # Code goes in here

[/code]

When using the __str__ method you have to make sure that you are returning a string. Here's an example of proper usage:

>>> class foo(object):
...     def __init__( self, name ):
...         self.name = name
...     def __str__( self ):
...         return self.name
...     
>>> mc = foo('bar')
>>> mc
<__main__.foo object at 0x01D24C30>
>>> print mc
bar
>>>

It sounds like you are returning something other than a string, but it's too hard to read your code without the usage of code tags. I suggest reading the forum guidelines and all posted announcements and then reposting your code and related dilemma.

EDIT: I see it now: Circle = ["Circle"] does not create a string. ["Circle"] is a list with a single element.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.