Advertisement
iKnight

Shape Calculator

Dec 11th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.66 KB | None | 0 0
  1. ###################################################
  2. ##  Name: Oliver Chapman                         ##
  3. ##  Date of Creation: 10/12/2016                 ##
  4. ##  Description: A program that can calaculate   ##
  5. ##               various measurements depending  ##
  6. ##               on the shape selected.          ##
  7. ##  Versiom: 0.1                                 ##
  8. ###################################################
  9.  
  10. ## Import modules, but give them a different identifier
  11. import math as m
  12. import os as o
  13. import sys as s
  14. import time as t
  15.        
  16. def __init__(): ## Defines the main function of the program
  17.  
  18.     result = 0.0 ## Sets the result value to 0.0
  19.  
  20.     o.system('cls') ## Clears the screen
  21.  
  22.     ## Prints the options on the screen
  23.     print("----====> Shape Calculator <====----")
  24.     print("\nPlease select an option.")
  25.     print("\n\n1) Area of a Circle\n\n2) Circumference of a Circle\n\n3) Area of a Rectangle\n\n4) Area of a Triangle\n\n5) Volume of a Cuboid\n\n6) Help\n\n0) Quit\n\n")
  26.  
  27.     try:
  28.         option = int(input("Option: ")) ## Attempts to get an input from the user and store it in 'option'
  29.     except ValueError:
  30.         print("\n\n!! Invalid Input Detected. !!") ## If the users input is invalid, display an error message
  31.         t.sleep(3) ## Pauses the program for 'x' amount of seconds (x = seconds)
  32.         __init__() ## Calls the __init__ function
  33.  
  34.     ## Checks to see what the user entered and handles it accordingly
  35.     if option == 1:
  36.         __cirArea(result) ## Call the __cirArea function with the result value passed (Call-by-reference)
  37.     elif option == 2:
  38.         __cirCircum(result)
  39.     elif option == 3:
  40.         __recArea(result)
  41.     elif option == 4:
  42.         __triArea(result)
  43.     elif option == 5:
  44.         __cubVol(result)
  45.     elif option == 6:
  46.         __help()
  47.     elif option == 0:
  48.         __quit()
  49.     else:
  50.         print("\n\n!! Please enter a number between 0 & 6. !!")
  51.         t.sleep(3)
  52.         __init__()
  53.  
  54. def __cirArea(result):
  55.  
  56.     radius = 0.0 ## Sets the radius to 0.
  57.     pi = m.pi ## Sets pi to the object pi from the math module
  58.  
  59.     o.system('cls')
  60.  
  61.     print("----====> Area of a Circle <====----")
  62.  
  63.  
  64.     try:
  65.         radius = float(input("\n\nRadius: "))
  66.     except ValueError:
  67.         print("\n\n!! Invalid Input Detected. !!")
  68.         t.sleep(3)
  69.         __cirArea(result)
  70.  
  71.     if radius >= 1 and radius <= 100: ## If the user inputted a number between 1 & 100
  72.         result = radius * pi ## Calculate the result
  73.         print("\n\nAnswer: ",result)
  74.         wait = input("\n\nPress enter to continue... ") ## Wait for the user to continue
  75.         __init__() ## Go back to the menu
  76.     else:
  77.         print("\n\n!! Please enter a number between 1 & 100. !!")
  78.         t.sleep(3)
  79.         __cirArea(result)
  80.  
  81. def __cirCircum(result):
  82.  
  83.     radius = 0.0
  84.     pi = m.pi
  85.  
  86.     o.system('cls')
  87.  
  88.     print("----====> Circumference of a Circle <====----")
  89.  
  90.     try:
  91.         radius = float(input("\n\nRadius: "))
  92.     except ValueError:
  93.         print("\n\n!! Invalid Input Detected. !!")
  94.         t.sleep(3)
  95.         __cirCircum(result)
  96.  
  97.     if radius >= 1 and radius <= 100:
  98.         result = pi * (radius * 2)
  99.         print("\n\nAnswer: ",result)
  100.         wait = input("\n\nPress enter to continue... ")
  101.         __init__()
  102.     else:
  103.         print("\n\n!! Please enter a number between 1 & 100. !!")
  104.         t.sleep(3)
  105.         __cirCircum(result)
  106.  
  107. def __recArea(result):
  108.  
  109.     length = 0.0 ## Sets the length to 0
  110.     width = 0.0 ## Sets the width to 0
  111.     pi = m.pi
  112.  
  113.     o.system('cls')
  114.  
  115.     print("----====> Area of a Rectangle <====----")
  116.  
  117.     try:
  118.         length = float(input("\n\nLength: "))
  119.     except ValueError:
  120.         print("\n\n!! Invalid Input Detected. !!")
  121.         t.sleep(3)
  122.         __recArea(result)
  123.  
  124.     try:
  125.         width = float(input("\n\nWidth: "))
  126.     except ValueError:
  127.         print("\n\n!! Invalid Input Detected. !!")
  128.         t.sleep(3)
  129.         __recArea(result)
  130.  
  131.     if length >= 1 and length <= 100 and width >= 1 and width <= 100:
  132.         result = length * width
  133.         print("\n\nAnswer: ",result)
  134.         wait = input("\n\nPress enter to continue... ")
  135.         __init__()
  136.     else:
  137.         print("\n\n!! Please enter a number between 1 & 100. !!")
  138.         t.sleep(3)
  139.         __recArea(result)
  140.  
  141. def __triArea(result):
  142.  
  143.     base = 0.0  ## Sets the base to 0
  144.     height = 0.0  ## Sets the height to 0
  145.  
  146.     o.system('cls')
  147.  
  148.     print("----====> Area of a Triangle <====----")
  149.  
  150.     try:
  151.         base = float(input("\n\nBase: "))
  152.     except ValueError:
  153.         print("\n\n!! Invalid Input Detected. !!")
  154.         t.sleep(3)
  155.         __triArea(result)
  156.  
  157.     try:
  158.         height = float(input("\n\nWidth: "))
  159.     except ValueError:
  160.         print("\n\n!! Invalid Input Detected. !!")
  161.         t.sleep(3)
  162.         __triArea(result)
  163.  
  164.     if base >= 1 and base <= 100 and height >= 1 and height <= 100:
  165.         result = 0.5 * (base * height)
  166.         print("\n\nAnswer: ", result)
  167.         wait = input("Press enter to continue... ")
  168.         __init__()
  169.     else:
  170.         print("\n\n!! Please enter a number between 1 & 100. !!")
  171.         t.sleep(3)
  172.         __triArea(result)
  173.  
  174. def __cubVol(result):
  175.  
  176.     length = 0.0  ## Sets the length to 0
  177.     width = 0.0  ## Sets the width to 0
  178.     height = 0.0  ## Sets the height to 0
  179.  
  180.     o.system('cls')
  181.  
  182.     print("----====> Volume of a Cuboid <====----")
  183.  
  184.     try:
  185.         length = float(input("\n\nLength: "))
  186.     except ValueError:
  187.         print("\n\n!! Invalid Input Detected. !!")
  188.         t.sleep(3)
  189.         __cubVol(result)
  190.  
  191.     try:
  192.         width = float(input("\n\nWidth: "))
  193.     except ValueError:
  194.         print("\n\n!! Invalid Input Detected. !!")
  195.         t.sleep(3)
  196.         __cubVol(result)
  197.  
  198.     try:
  199.         height = float(input("\n\nHeight: "))
  200.     except ValueError:
  201.         print("\n\n!! Invalid Input Detected. !!")
  202.         t.sleep(3)
  203.         __cubVol(result)
  204.  
  205.     if length >= 1 and length <= 100 and width >= 1 and width <= 100 and height >= 1 and height <= 100:
  206.         result = 0.5 * (base * height)
  207.         print("\n\nAnswer: ", result)
  208.         wait = input("Press enter to continue... ")
  209.         __init__()
  210.     else:
  211.         print("\n\n!! Please enter a number between 1 & 100. !!")
  212.         t.sleep(3)
  213.         __cubVol(result)
  214.  
  215. def __quit():
  216.  
  217.     secs = 5
  218.     uQuit = 'n'
  219.  
  220.     o.system('cls')
  221.  
  222.     print("----====> Quit <====----")
  223.  
  224.     try:
  225.         uQuit = input("\n\nAre you sure you want to quit [Y/N]: ")
  226.         uQuit = uQuit.upper() ## Sets the user input to uppercase
  227.     except ValueError:
  228.         print("\n\n!! Invalid Input Detected. !!")
  229.         t.sleep(3)
  230.         __quit()
  231.  
  232.     if uQuit == "Y":
  233.         for counter in range(secs): ## Run until the counter is equivalent to secs variable
  234.             o.system('cls')
  235.  
  236.             print("Closing in", secs,"...")
  237.             secs = secs -1
  238.             t.sleep(1)
  239.  
  240.             if secs == 0: ## If secs is 0, close the program
  241.                 print("\n\nGoodbye!")
  242.                 t.sleep(3)
  243.                 s.exit()
  244.     elif uQuit == "N":
  245.         __init__()
  246.     else:
  247.         print("\n\n!! Please enter either Y or N. !!")
  248.         t.sleep(3)
  249.         __quit()
  250.  
  251. def __help():
  252.  
  253.     help  = 0
  254.  
  255.     o.system('cls')
  256.  
  257.     print("      ----====> Help <====----")
  258.  
  259.     print("\nThis is the help menu. Here you can select\na calculation that you want help with.")
  260.     print("\n\n1) Area of a Circle\n\n2) Circumference of a Circle\n\n3) Area of a Rectangle\n\n4) Area of a Triangle\n\n5) Volume of a Cuboid\n\n6) Main Menu")
  261.  
  262.     try:
  263.         help = int(input("\n\nOption: "))
  264.     except ValueError:
  265.         print("\n\n!! Invalid Input Detected !!")
  266.         t.sleep(3)
  267.         __help()
  268.  
  269.     if help == 1:
  270.         __cirArHelp()
  271.     elif help == 2:
  272.         __cirCiHelp()
  273.     elif help == 3:
  274.         __recArHelp()
  275.     elif help == 4:
  276.         __triArHelp()
  277.     elif help == 5:
  278.         __cubVoHelp()
  279.     elif help == 6:
  280.         __init__()
  281.     else:
  282.         print("\n\n!! Please enter a number between 1 & 6. !!")
  283.         t.sleep(3)
  284.         __help()
  285.  
  286. def __cirArHelp():
  287.     o.system('cls')
  288.  
  289.     print("      ----====> Area of a Circle <====-----")
  290.     print("\nEnter the radius. The program will then square it\nand multiply it by pi.\nThe answer will be displayed in centimeters squared.")
  291.    
  292.     wait = input("\n\nPress enter to continue... ")
  293.     __help()
  294.  
  295. def __cirCiHelp():
  296.     o.system('cls')
  297.  
  298.     print("      ----====> Circumference of a Circle <====-----")
  299.     print("\nEnter the radius. The program will then double it\n& multiply it by pi.\nThe answerwill be displayed in centimeters squared.")
  300.    
  301.     wait = input("\n\nPress enter to continue... ")
  302.     __help()
  303.  
  304. def __recArHelp():
  305.     o.system('cls')
  306.  
  307.     print("      ----====> Area of a Rectangle <====-----")
  308.     print("\nEnter the length & width. The program will then multiply\nthe length & width together.\nThe answer will be displayed in centimeters squared.")
  309.    
  310.     wait = input("\n\nPress enter to continue... ")
  311.     __help()
  312.  
  313. def __triArHelp():
  314.     o.system('cls')
  315.  
  316.     print("      ----====> Area of a Triangle <====-----")
  317.     print("\nEnter the base & height. The program will then \nmultiply the base & height and halve it.\nThe answer will be displayed in centimeters squared.")
  318.    
  319.     wait = input("\n\nPress enter to continue... ")
  320.     __help()
  321.  
  322. def __cubVoHelp():
  323.     o.system('cls')
  324.  
  325.     print("      ----====> Volume of a Cuboid <====-----")
  326.     print("\nEnter the base, width & height.\nThe program will then square it\n& multiply all three numbers together.\nThe answer will be displayed in centimeters cubed.")
  327.    
  328.     wait = input("\n\nPress enter to continue... ")
  329.     __help()
  330.  
  331. __init__()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement