Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.18 KB | None | 0 0
  1. # This is my volume calculator program
  2.  
  3.  
  4. from volumes import *     # imports the cubeVolume function, the pyramidVolume function, and the ellipsoidVolume function
  5.  
  6. ## VARIABLES ##
  7.  
  8. # list variables
  9. cubeList = []
  10. pyramidList = []
  11. ellipsoidList = []
  12.  
  13. # variables from the given summarize function
  14. NEWLINE = "\n"
  15. DASH = "-"
  16. SPACE = " "
  17.  
  18. # boolean variable that checks if testNumber input is a number
  19. isNotNum = True
  20. #
  21. ##
  22.  
  23.  
  24. testNumber = input("What test number do you want assigned for your calculations? ")
  25. if testNumber.isnumeric():     # if statement to test if input for testNumber is a number
  26.     isNotNum = True            # if testNumber is a number, the program will continue to shape
  27. else:     # else statement if testNumber is NOT a number
  28.     while isNotNum:      # while loop that will continue if the user does not enter a number
  29.         testNumber = input("That is an invalid input! Please enter a number to be assigned to your volume calculations. ")
  30.         if testNumber.isnumeric():
  31.             isNotNum = False
  32.  
  33. shape = ""     # "" is used to identify shape as a string
  34. while shape != "quit" or "q":     # while the user does NOT input quit/q, this loop will ask the user for a shape
  35.     shape = input("Please enter what shape you would like \nValid inputs are cube or c, pyramid or p, ellipsoid or e. Otherwise enter quit or q to stop ").replace(" ", "").lower()
  36.     # while loop when user does not input valid input
  37.     while shape != "cube" and shape != "c" and shape != "pyramid" and shape != "p" and shape != "ellipsoid" and shape != "e" and shape != "quit" and shape != "q":
  38.         print("That is not a valid input. Please choose from cube/c, pyramid/p, ellipsoid/e. Otherwise enter quit/q to stop.")
  39.         shape = input("Please enter what shape you would like \nValid inputs are cube/c, pyramid/p, ellipsoid/e or enter quit/q to stop: ").replace(" ", "").lower()
  40.  
  41.  
  42.     if shape == "cube" or shape == "c":                                 # if the user enters cube or c, the program will run this statement
  43.         sideLength = float(input("Please enter the side length: "))
  44.         print(" ")     # this print statement helps format the program to not look so cluttered
  45.         vol = cubeVolume(sideLength)                                    # variable to store the return value from the function
  46.         cubeList.append(vol)                                            # .append(vol) adds the value of the calculation to the end of the cubeList in this case
  47.         cubeList.sort()                                                 # .sort() will sort the values in the cubeList from smallest to highes
  48.  
  49.     elif shape == "pyramid" or shape == "p":                            # if the user enters pyramid or p, the program will run this statement
  50.         baseLength = float(input("Please enter the base length: "))
  51.         height = float(input("Please enter the height: "))
  52.         print(" ")  
  53.         vol = pyramidVolume(baseLength, height)                    
  54.         pyramidList.append(vol)                                      
  55.         pyramidList.sort()
  56.  
  57.     elif shape == "ellipsoid" or shape == "e":                               # if the user enters ellipsoid or e, the program will run this statement
  58.         radius1 = float(input("Please enter the first radius length: "))
  59.         radius2 = float(input("Please enter the second radius length: "))
  60.         radius3 = float(input("Please enter the last radius length: "))
  61.         print(" ")
  62.         vol = ellipsoidVolume(radius1, radius2, radius3)
  63.         ellipsoidList.append(vol)
  64.         ellipsoidList.sort()
  65.  
  66.     else:     # when the user enters quit/q, the program will show the results if there are any
  67.         print("You have come to the end of your session. \nThe volumes of the shape(s) are shown below.")
  68.         print(" ")     # this print statement helps format the program to not look so cluttered
  69.         if not cubeList and not pyramidList and not ellipsoidList:                              # if statement if the user didn't ask for any calculations
  70.             print("You didn't ask for any calculations. \nThere are no calculated values.")
  71.             quit(0)     # this will stop the program when there are no calculated values
  72.         else:
  73.             if not cubeList:     # if statement when there are no volume calculations for cubes
  74.                 print("Cube: There are no calculations.")
  75.             elif cubeList:
  76.                 for i in cubeList:                            # this removes the [] in the list
  77.                     print("Cube:", "%.2f" % i)                # this print statement will format the output to 2 decimal places
  78.             if not pyramidList:     # if statement when there are no volume calculations for pyramids
  79.                 print("Pyramid: There are no calculations.")
  80.             elif pyramidList:
  81.                 for i in pyramidList:
  82.                     print("Pyramid:", "%.2f" % i)
  83.             if not ellipsoidList:     # if statement when there are no volume calculations for ellipsoids
  84.                 print("Ellipsoid: There are no calculations.")
  85.             elif ellipsoidList:
  86.                 for i in ellipsoidList:
  87.                     print("Ellipsoid:", "%.2f" % i)
  88.             quit(0)     # this will stop the program when everything is done
  89.  
  90.  
  91. #
  92. # Assignment #2
  93. # Output of list elements for testing program
  94. #
  95.  
  96. # Function summarize simply outputs the lists for each shape with 2 decimal place accuracy
  97. #
  98.  
  99. # below is the summarize function given for assignment 2
  100. #
  101.  
  102.     def summarize(clist,plist,elist,testNumber):
  103.         fname = "Asgn2TestCase" + str(testNumber)
  104.         print()
  105.         print("*** Creating output for test "+ str(testNumber))
  106.         print()
  107.         outf = open(fname,"w")
  108.         output_list(clist,outf)
  109.         output_list(plist,outf)
  110.         output_list(elist,outf)
  111.         outf.close()
  112.  
  113.     def output_list(lst,outfile):
  114.         lngth = len(lst)
  115.         if lngth > 0:
  116.             for item in lst:
  117.                 str = "%6.2f" % item
  118.                 outfile.write(str+SPACE)
  119.             outfile.write(NEWLINE)
  120.         else:
  121.             outfile.write(DASH+NEWLINE)
  122.  
  123.     summarize(cubeList, pyramidList, ellipsoidList, testNumber)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement