Question

Branching, Looping, Import user_defined module, functions, docstring

Topics: Branching, Looping, Import user_defined module, functions, docstring Problem Statement: Geometric Calculator In this BWA, we will be building a simple geometric calculator using user-defined functions and import module procedure. The user will have options to choose different shapes like triangle, circle, square, rectangle and what they want to calculate: for e.g. perimeter, area etc. The calculator will provide them the results according to the user choice. Apart from the usage of module and functions, this as- signment also focuses on documenting the code. Therefore, your code should be well documented with docstring(will be discussed in lecture 14), comments and pseudocode. You also have to account for edge cases when a user enters garbage/wrong inputs.

Topics: Branching, Looping, Import user_defined module, functions, docstring Problem Statement: Geometric Calculator In this

1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
import math

class Rectangle:

  def __init__(self,length,width):
    self.length = length
    self.width = width
  
  def perimeter(self):
    return 2*(self.length + self.width)

  def area(self):
    return self.length*self.width

class Square:

  def __init__(self,length):
    self.length = length
  
  def perimeter(self):
    return 4*(self.length)

  def area(self):
    return self.length**2

class Triangle:

  def __init__(self,s1,s2,s3):
    self.s1 = s1
    self.s2 = s2
    self.s3 = s3
  
  def perimeter(self):
    return self.s1 + self.s2 + self.s3
  
  def area(self):
    s = (self.s1 + self.s2 + self.s3)/2;
    return math.sqrt(s*(s-self.s1)*(s-self.s2)*(s-self.s3))

class Circle:

  def __init__(self,radius):
    self.radius = radius
  
  def perimeter(self):
    return 2*(self.radius)*math.pi

  def area(self):
    return self.radius*self.radius*math.pi


while 1:
  option = input('Choose option \n 1.Calculator 2.Exit')
  if int(option) == 2:
    break
  else:
    shape = input('Enter shape name')
    if (shape == 'triangle'):
      typeoption = int(input('Enter 1.Perimter 2. Area'))
      
      s1 = input('enter side 1:')
      s2 = input('enter side 2:')
      s3 = input('enter side 3:')
      s1 = int(s1)
      s2 = int(s2)
      s3 = int(s3)
      triangle = Triangle(s1,s2,s3)
      if typeoption == 1:
        print(triangle.perimeter())
      else:
        print(triangle.area())
    elif (shape == 'rectangle'):
      typeoption = int(input('Enter 1.Perimter 2. Area'))
      s1 = int(input('enter side 1:'))
      s2 = int(input('enter side 2:'))
      rectangle = Rectangle(s1,s2)
      if typeoption == 1:
        print(rectangle.perimeter())
      else:
        print(rectangle.area())
    elif (shape == 'square'):
      typeoption = int(input('Enter 1.Perimter 2. Area'))
      s1 = int(input('enter side:'))
      square = Square(s1)
      if typeoption == 1:
        print(square.perimeter())
      else:
        print(square.area())
    elif (shape == 'circle'):
      typeoption = int(input('Enter 1.Perimter 2. Area'))
      s1 = int(input('enter side:'))
      circle = Circle(s1)
      if typeoption == 1:
        print(circle.perimeter())
      else:
        print(circle.area())


answered by: Zahidul Hossain
Add a comment
Know the answer?
Add Answer to:
Branching, Looping, Import user_defined module, functions, docstring
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Please show all work and answer all parts using python, thanks! Instructions You will need to...

    Please show all work and answer all parts using python, thanks! Instructions You will need to create four files: • Shape2D.py - file containing a class definition containing properties all Shapes could possibly have. • Circle.py - file containing a class definition of a Circle that inherits from the Shape2D class. Square.py - file containing a class definition of a Square that inherits from the Shape2D class. • testFile.py - file containing pytest functions testing the Shape2D, Circle, and Square...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT