fork(1) download
  1. import math
  2. import random
  3.  
  4. COLOURS = ['black', 'yellow', 'red', 'magenta', 'cyan', 'purple']
  5.  
  6. class Square(object):
  7. def __init__(self, side, colour="black"):
  8. self.side = side
  9. self.colour = colour
  10.  
  11. @classmethod
  12. def generate_random(cls):
  13. side = random.random() * 100
  14. colour = COLOURS[random.randint(0, len(COLOURS) - 1)]
  15. return cls(side, colour)
  16.  
  17. @property
  18. def area(self):
  19. return self.side ** 2
  20.  
  21. @property
  22. def perimeter(self):
  23. return self.side * 4
  24.  
  25. def __repr__(self):
  26. return "%s square with the side: %.2f, area: %.2f, perimeter: %.2f" % (
  27. self.colour.title(), self.side, self.area, self.perimeter)
  28.  
  29. class Circle(object):
  30. def __init__(self, radius, colour="black"):
  31. self.radius = radius
  32. self.colour = colour
  33.  
  34. @classmethod
  35. def generate_random(cls):
  36. radius = random.random() * 100
  37. colour = COLOURS[random.randint(0, len(COLOURS) - 1)]
  38. return cls(radius, colour)
  39.  
  40. @property
  41. def area(self):
  42. return self.radius ** 2 * math.pi
  43.  
  44. @property
  45. def perimeter(self):
  46. return self.radius * 2 * math.pi
  47.  
  48. def __repr__(self):
  49. return "%s circle with the radius: %.2f, area: %.2f, perimeter: %.2f" % (
  50. self.colour.title(), self.radius, self.area, self.perimeter)
  51.  
  52. classes = [Square, Circle]
  53.  
  54.  
  55. figs = []
  56. numfigs = 10
  57. for i in range(numfigs):
  58. fig = classes[random.randint(0, len(classes) - 1)]
  59. figs.append(fig.generate_random())
  60.  
  61.  
  62. for fig in figs:
  63. print(fig)
Success #stdin #stdout 0.03s 12360KB
stdin
Standard input is empty
stdout
Cyan square with the side: 25.46, area: 648.45, perimeter: 101.86
Black square with the side: 31.15, area: 970.18, perimeter: 124.59
Black square with the side: 64.85, area: 4205.88, perimeter: 259.41
Black circle with the radius: 58.15, area: 10623.88, perimeter: 365.38
Yellow square with the side: 65.48, area: 4287.10, perimeter: 261.90
Black circle with the radius: 12.90, area: 522.62, perimeter: 81.04
Black circle with the radius: 42.91, area: 5784.75, perimeter: 269.62
Yellow circle with the radius: 58.98, area: 10928.17, perimeter: 370.58
Yellow circle with the radius: 66.43, area: 13862.94, perimeter: 417.38
Red square with the side: 98.27, area: 9656.36, perimeter: 393.07