Skip to content

Instantly share code, notes, and snippets.

@davegotz
Created April 16, 2019 14:08
Show Gist options
  • Select an option

  • Save davegotz/0c2c3f059af849a3dcb356a87d3bc2e8 to your computer and use it in GitHub Desktop.

Select an option

Save davegotz/0c2c3f059af849a3dcb356a87d3bc2e8 to your computer and use it in GitHub Desktop.
import random
class Shape:
def __init__(self, shape_name, shape_width, shape_symbol):
self.name = shape_name
self.width = shape_width
self.symbol = shape_symbol
def __str__(self):
return self.name + " of size " + str(self.width) + " and area " + str(self.area())
# This needs to be overridden
def draw(self):
print("Draw shape!")
# This needs to be overridden
def area(self):
return 0
class Rectangle(Shape):
def __init__(self, shape_width, shape_height, shape_symbol):
Shape.__init__(self, "Rectangle", shape_width, shape_symbol)
self.height = shape_height
def __str__(self):
return self.name + " of size " + str(self.width) + "x" + str(self.height) + " and area " + str(self.area())
def draw(self):
for row in range(self.height):
print(self.width * self.symbol)
def area(self):
return self.height * self.width
class Square(Shape):
def __init__(self, shape_width, shape_symbol):
Shape.__init__(self, "Square", shape_width, shape_symbol)
def draw(self):
for row in range(self.width):
print(self.width * self.symbol)
def area(self):
return self.width * self.width
class Triangle(Shape):
def __init__(self, shape_width, shape_symbol):
Shape.__init__(self, "Triangle", shape_width, shape_symbol)
def draw(self):
for row in range(1,self.width+1):
print(row * self.symbol)
def area(self):
return 0.5 * self.width * self.width
def main():
my_shapes = [Rectangle(5, 14, 'x'), Square(3, '*'), Triangle(4, '#')]
random.shuffle(my_shapes)
for shape in my_shapes:
print(shape)
shape.draw()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment