I'm currently trying to code this area calulator but I'm having a hard time understanding how to implement a while loop, any help would be appreciated. I have tried looking into loops but I'm not quite understanding how to write them even after watching some YouTube videos.
def calculate_square_area(side:float):
return side * side
def calculate_rectangele_area(length:float, width:float):
return length * width
def calculate_circle_area(radius:float):
pi = 3.14
return pi * radius **2
def calculate_rhombus_area(p: float, q: float):
return p*q/2
print("""
---------------
Area Calculator
---------------
""")
selection = input("""\t 'S' - Square
\t 'R'- Rectangle
\t 'C'- Circle
\t 'H'- Rhombus
""")
def calculate_area(selection):
area = 0
if selection == "S":
side =input("Enter The Side:")
area = calculate_square_area(float(side))
elif selection == "R":
length = input ("Enter The Length:")
width = input ("Enter The Width:")
area = calculate_rectangele_area(float(length), float(width))
elif selection == "C":
radius = input ("Enter The Radius:")
area = calculate_circle_area(float(radius))
elif selection == "H":
p = input("Enter P:")
q = input ("Enter Q:")
area = calculate_rhombus_area(float(p), float(q))
else:
print ("Invalid selection")
return area
def get_shape_name(tag):
shape = "Unkown"
if tag =="S":
shape = "Square"
elif tag == "R":
shape ="Rectangle"
elif tag == "C":
shape = "Circle"
elif tag == "H":
shape = "Rhombus"
return shape
area = calculate_area(selection)
print(f"The area of the {get_shape_name(selection)} is {area}")
source https://stackoverflow.com/questions/75302476/how-to-implement-a-while-loop-into-this-code
Comments
Post a Comment