In: Computer Science
Garden Plot Calculator
Assume that you have a garden plot like the following:
The blue circle represents a fountain and the orange part the flower bed. Remember that the area of a square is the length of oneof its sides squared. Also, the area of a circle is πr, where r is the radius of the circle. The volume is the square footage times thedepth.For this project, you get to write a Python script to perform some calculations for the garden plot, based on user input.
Write a Python 3 script that has functions do the following:
Welcome the user
Prompts the user for the length of one of the sides of the garden and returns that value
Prompts the user for the radius of the fountain and returns that value
Calculates the total square footage of the flower bed
Prompts the user for the depth of the flower bed and returns that value
Have your script display the total square footage of the garden plot, the square footage of just the flower bed and the amount of soil needed for the flower bed (in cubic foot)
. Use the math module for π.
Also, your script must appropriately include a main() function, which cannot be the longest function.
Additionally, round all calculations to 1 decimal place. Include a descriptive comment before each function and each major section of your code.
Ans: I have opted to use a class based python script to solve the given problem but if needed it can be easily converted to the function based python program by using instance members (ex:- length, depth, etc) in the class as global variables in function based program.
Code: Please read comments for code explanation.
import math
# GarednPlot contains all the utility functions
class GardenPlot:
# constructor function: Welcomes the user and sets default values
def __init__(self):
print("Welcome!")
self.length = 0
self.radius = 0
self.depth = 0
# Sets the length of the GardenPlot and returns that length
def setLength(self):
self.length = float(input("Enter length of the garden: "))
return self.length
# Sets the radius of the Fountain in the GardenPlot and returns that radius
def setRadius(self):
self.radius = float(input("Enter radius of the fountain: "))
return self.radius
# Sets the depth of the GardenPlot/Flower-bed and returns that depth
def setDepth(self):
self.depth = float(input("Enter depth of the flower bed: "))
return self.depth
# Returns the total area of the GardenPlot using the given formula
def area_of_garden(self):
return round((self.length * self.length), 1)
# Returns the total area of the Fountain in the GardenPlot using the given formula
def area_of_fountain(self):
# round() rounds of the output to 1 decimal place
return round((math.pi * self.radius * self.radius), 1)
# Returns the total area of the flower bed
def area_of_flowerbed(self):
# Area of Flower-bed = Area of GardenPlot - Area of Fountain
return round((self.area_of_garden() - self.area_of_fountain()), 1)
# Returns the volume of the flower bed using the given formula
def volume_of_soil(self):
volume = self.area_of_flowerbed() * self.depth
return round(volume, 1)
# Prints the final stats of the Garden Plot
def main(self):
print("Total area of garden plot: "+ str(self.area_of_garden()))
print("Total area of flower bed: "+ str(self.area_of_flowerbed()))
print("Total amount of soil needed: "+ str(self.volume_of_soil()))
# Driver function
if __name__ == "__main__":
# Create the object
plot = GardenPlot()
# Prompts user and displays all the inputs one after the other
length = plot.setLength()
print("Length: " + str(length))
radius = plot.setRadius()
print("Radius: " + str(radius))
depth = plot.setDepth()
print("Depth: " + str(depth))
# Print stats
plot.main()