"""Calculate the area and perimeter of a given shape. this program will allow users to pick a shape and then calculate its area and perimeter """ import math import pandas as pd import numpy as np # functions go here... def make_statement(statement, decoration, lines): """Create headings, subheadings, and mini headings. Only use emoji for single line statements """ middle = f"{decoration * 3} {statement} {decoration * 3}" top_bottom = decoration * len(middle) if lines == 1: print(middle) elif lines == 2: print(middle) print(top_bottom) else: print(top_bottom) print(middle) print(top_bottom) # Functions go here def string_check(question, valid_ans_list, num_letters): """Check that users enter a full word. or the 'n' letter/s of a word from a list of valid responses """ while True: response = input(question).lower() for item in valid_ans_list: if response == item: return item elif response == item[:num_letters]: return item print(f"Please choose an option from the list {valid_ans_list}") # Functions go here def int_check(question, low, high): """Check that users enter the integer value within a specified boundary.""" error = (f"Oops - please enter a number that is higher " f"or equal to {low} and lower or equal to {high} .") while True: try: response = float(input(question)) if low <= response <= high: return response else: print(error) except ValueError: print(error) # All Lists instructions = ["yes", "no"] shape_list = ["circle", "square", "rectangle", "parallelogram"] exit_code = ["continue", "exit"] data_list = [] # Instructions checker want_instructions = string_check("Do you want to read the instructions? yes(y) or no(n): ", instructions, 1) # Instructions loop if want_instructions == "yes": make_statement("Instructions", "=", 3) print() make_statement( "Pick a shape, " "then input the side sizes!", "-", 2, ) print() make_statement( "The calculator will find the area and perimeter ", "!", 1, ) elif want_instructions == "no": print("you chose no") # Main loop while True: # Shape Selection shape_select = string_check("Pick a shape from the list " "[circle (c), square (s), rectangle (r), parallelogram (p)]: ", shape_list, 1) print(f"You chose {shape_select}") # Circle calculations if shape_select == shape_list[0]: radius = int_check("Enter the radius of your shape: ", 0.1, 100) circumference = 2 * math.pi * radius area = math.pi * radius * radius shape = 'circle' circumference_round = round(circumference, 2) area_round = round(area, 2) print(f' The circumference of the circle is:{circumference_round}') print(f' The area of the circle is:{area_round}') data_list.append({ "Shape": shape, "Perimeter": circumference_round, "Area": area_round }) leave = string_check("Exit(e) or continue(c)? ", exit_code, 1) # Square calculations elif shape_select == shape_list[1]: length = int_check("enter the length of your shape: ", 0.1, 100) area = length * length shape = 'square' perimeter = length + length + length + length area_round = round(area, 2) perimeter_round = round(perimeter, 2) print(f' The area of the square is:{area_round}') print(f' The perimeter of the square is:{perimeter_round}') data_list.append({ "Shape": shape, "Area": area_round, "Perimeter": perimeter_round }) leave = string_check("Exit(e) or continue(c)? ", exit_code, 1) # Rectangle calculations elif shape_select == shape_list[2]: first_length = int_check("enter the length of your shape: ", 0.1, 100) second_length = int_check("enter the width of your shape: ", 0.1, 100) area = first_length * second_length perimeter = first_length + first_length + second_length + second_length shape = 'rectangle' area_round = round(area, 2) perimeter_round = round(perimeter, 2) print(f' The area of the rectangle is:{area_round}') print(f' The perimeter of the rectangle is:{perimeter_round}') data_list.append({ "Shape": shape, "Area": area_round, "Perimeter": perimeter_round }) leave = string_check("Exit(e) or continue(c)? ", exit_code, 1) # Parallelogram calculations elif shape_select == shape_list[3]: length = int_check("enter the length of your shape: ", 0.1, 100) height = int_check("enter the height of your shape: ", 0.1, 100) area = length * height perimeter = length + length + length + length shape = 'parallelogram' area_round = round(area, 2) perimeter_round = round(perimeter, 2) print(f' The area of the parallelogram is:{area_round}') print(f' The perimeter of the parallelogram is:{perimeter_round}') data_list.append({ "Shape": shape, "Area": area_round, "Perimeter": perimeter_round }) leave = string_check("Exit(e) or continue(c)? ", exit_code, 1) # Exit loop if leave == "continue": continue elif leave == "exit": # Prints calculation history table and then breaks df = pd.DataFrame(data_list) df.index = np.arange(1, len(df) + 1) print('Calculation History:') print(df) break