Advertisement
Ihmemies

4-8-0

Sep 11th, 2022 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | Source Code | 0 0
  1. import math
  2.  
  3. def ask_q(q):
  4.     """
  5.    asks above zero input from user
  6.    returns input
  7.    """
  8.     user_input = float
  9.  
  10.     while True:
  11.         user_input = float(input(q))
  12.         if 0 >= user_input:
  13.             continue
  14.         else:
  15.             break
  16.  
  17.     return user_input
  18.  
  19. def print_results(res):
  20.     """
  21.    prints results from list. assumes list has 2 entries.
  22.    no returns
  23.    """
  24.     print(f"The circumference is {res[0]:.2f}")
  25.     print(f"The surface area is {res[1]:.2f}")
  26.  
  27. def calc_area_s():
  28.     """
  29.    calculates square circ&area
  30.    no params
  31.    no returns
  32.    """
  33.     res = []
  34.     len = ask_q("Enter the length of the square's side: ")
  35.    
  36.     res.append(len*4)    # circumference    
  37.     res.append(len*len)  # area
  38.     print_results(res)
  39.  
  40. def calc_area_r():
  41.     """
  42.    calculates rect circ&area
  43.    no params
  44.    no returns
  45.    """
  46.     res = [0, 0]
  47.     lens = []
  48.     i = 1
  49.  
  50.     while i < 3:
  51.         lens.append(ask_q(f"Enter the length of the rectangle's side {i}: "))
  52.         i += 1
  53.  
  54.     res[0] = lens[0]*2 + lens[1]*2
  55.     res[1] = lens[0] * lens[1]
  56.  
  57.     print_results(res)
  58.  
  59. def calc_area_c():
  60.     """
  61.    calculates circle circ&area
  62.    no params
  63.    no returns
  64.    """
  65.     res = []
  66.     len = ask_q("Enter the circle's radius: ")
  67.    
  68.     res.append(2 * math.pi * len)  # circumference    
  69.     res.append(math.pi*(len*len))  # area
  70.     print_results(res)
  71.  
  72. def menu():
  73.     """
  74.    Print a menu for user to select which geometric pattern to use in calculations.
  75.    """
  76.     while True:
  77.         answer = input("Enter the pattern's first letter or (q)uit: ")
  78.  
  79.         if answer == "s":
  80.             calc_area_s()  
  81.         elif answer == "r":
  82.             calc_area_r()  
  83.         elif answer == "c":
  84.             calc_area_c()  
  85.         elif answer == "q":
  86.             return
  87.  
  88.         else:
  89.             print("Incorrect entry, try again!")
  90.  
  91.         # Empty row for the sake of readability.
  92.         print()
  93.  
  94. def main():
  95.     menu()
  96.     print("Goodbye!")
  97.  
  98. if __name__ == "__main__":
  99.     main()
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement