Reputation: 87
This is my homework:
Write a program that computes the area of a square, triangle, circle, and a rectangle. The program then prints the output in nicely formatted table as shown in the example below. Your program should be implemented with at least 3 function and docstrings. You should create a new module “area_tools” with the following functions in it:
Square(base)
,Triangle(base,height)
,Circle(radius)
, andRectangle(base,height)
. Your main program should use the functions in your module.Example of the table: Vars | square | Triangle | circle | rectangle 10,10 | 100 | ??? | 345.56 | 4567
I know I did it wrong and I don't know how to go from here. I get an error message saying:
TypeError: Square() missing 1 required positional argument: 'base'
I really don't know what to do. This is what I've done so far. In a file named area_tools.py, I did this:
def Triangle():
'''Finds the area of the triangle'''
base = int(input("Enter the base: "))
height = int(input("Enter the height: "))
triangle_area = (base * height) / 2
return triangle_area
def Rectangle():
'''Finds the area of the rectangle'''
base = int(input("Enter the base: "))
height = int(input("Enter the height: "))
rectangle_area = base * height
return rectangle_area
def Circle():
'''Finds the area of the circle'''
radius = int(input("Enter the radius: "))
pi = 3.14159
circle_area = pi * radius ** 2
return circle_area
def Square():
base = int(input("Enter the base: "))
square_area = base ** 2
return square_area
In a second file, I did this:
import area_tools
import pandas as pd
data = {'square': [Square()],
'triangle': [Triangle()],
'circle': [Circle()],
'rectangle': [Rectangle()],
}
df = pd.DataFrame(data,columns=['square', 'triangle', 'circle','rectangle'])
df
EDIT: I copied and pasted the code on here so it's easier to see what I'm talking about: https://repl.it/@fgffdsfj/StrangeInfantileDisks#main.py
Upvotes: 0
Views: 248
Reputation: 142641
You should use input()
outside your functions and send values as arguments
def Triangle(base, height):
'''Finds the area of the triangle'''
return base * height / 2
def Rectangle(base, height):
'''Finds the area of the rectangle'''
return base * height
def Circle(radius):
'''Finds the area of the circle'''
pi = 3.14159
return pi * radius ** 2
def Square(base):
return base ** 2
my_base = int(input("Enter the base/width/radius: "))
my_height = int(input("Enter the height: "))
#my_radius = int(input("Enter the radius: "))
# only for fast test
#my_base = 3
#my_height = 4
result_square = Square(my_base)
result_triangle = Triangle(my_base, my_height)
result_circle = Circle(my_base)
result_rectangle = Rectangle(my_base, my_height)
And later you can use string formatting (align to left, right or center) to create table.
print('| {:^7} | {:^10} | {:^10} | {:^10} | {:^10} |'.format('Vars', 'Square', 'Triangle', 'Circle', 'Rectangle'))
print('| {:>3},{:<3} | {:^10} | {:^10} | {:^10} | {:^10} |'.format(my_base, my_height, result_square, result_triangle, result_circle, result_rectangle))
Result
| Vars | Square | Triangle | Circle | Rectangle |
| 3,4 | 9 | 6.0 | 28.27431 | 12 |
More about string formatting on PyFormat.info
Upvotes: 1
Reputation: 70
In the Second file when you call function your didn't send any argument to function that why it return an error
You can remove argument to function in First file and get from input instead
def Triangle():
'''Finds the area of the triangle'''
base = int(input("Enter the base: "))
height = int(input("Enter the height: "))
triangle_area = (base * height) / 2
return triangle_area
def Rectangle():
'''Finds the area of the rectangle'''
base = int(input("Enter the base: "))
height = int(input("Enter the height: "))
rectangle_area = base * height
return rect_area
def Circle():
'''Finds the area of the circle'''
radius = int(input("Enter the radius: "))
circle_area = pow(math.pi * radius, 2)
return circle_area
Upvotes: 2