Advertisement
Guest User

final ver

a guest
Jun 24th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #I tried to start with Row 1 instead of 0 in the lists, but for some reason repl.it would return "IndexError: list index out of range" when trying to print it, so I had to stick with starting at Row 0. Please look at this if you can to see if I could have made it work somehow.
  2.  
  3. #Joseph Shaw
  4. #Menu driven program
  5.  
  6.  
  7. # create a list with given values
  8. list = [3,4,84,5,2,47,7]
  9. # sorting of list
  10. list.sort()
  11. #print choices
  12. print("1. factorial \n2. median \n3. average \n4. maximum \n5. minimum \n6. exit\n")
  13.  
  14. #create stringArray for functions
  15. stringArray = []
  16.  
  17. #create answersArray for answers
  18. answerArray = []
  19.  
  20. # function to calculate factorial of 20
  21. def factorial(n):
  22. fact = 1
  23. # calculate factorial of 20
  24. for i in range (1,21):
  25. fact = fact * i
  26. # print factorial
  27. print("factorial is : ",fact)
  28. stringArray.insert(0, 'Factorial')
  29. answerArray.insert(0, fact)
  30. #print(stringArray[0])
  31. #print(answerArray[0])
  32.  
  33.  
  34. # function of find median of list (middle value in array)
  35. def median(list):
  36. # find median of list
  37. median = list[(len(list)-1)//2]
  38. #print median
  39. print("median is : ",median)
  40. stringArray.insert(1, 'Median')
  41. answerArray.insert(1, median)
  42.  
  43.  
  44. # function to calculate average
  45. def average(list):
  46. # sum(answerArray) for sum of list and len(answerArray) for length of list
  47. avg = sum(list)/len(list)
  48. #print average
  49. print("average is : ",avg)
  50. stringArray.insert(2, 'Average')
  51. answerArray.insert(2, avg)
  52.  
  53.  
  54. # function to find maximum number in list (last element in sorted list)
  55. def maximum(list):
  56. # find maximum number in list
  57. max = list[len(list)-1]
  58. # print maximum number
  59. print("maximum is : ",max)
  60. stringArray.insert(3, 'Maximum')
  61. answerArray.insert(3, max)
  62.  
  63.  
  64. # function to find minimum number in list (first element in sorted list)
  65. def minimum(list):
  66. # find minimum number in list
  67. min = list[0]
  68. # print minimum number
  69. print("minimum is : ",min)
  70. stringArray.insert(4, 'Minimum')
  71. answerArray.insert(4, min)
  72.  
  73. while True:
  74. # enter choice
  75. choice = int(input("Enter your choice \n"))
  76. # for factorial of 20
  77. if choice == 1:
  78. n=20
  79. factorial(n)
  80. # for median of list
  81. elif choice == 2 :
  82. median(list)
  83. # for average of list
  84. elif choice == 3 :
  85. average(list)
  86. # for maximum of list
  87. elif choice == 4 :
  88. maximum(list)
  89. # for minimum of list
  90. elif choice == 5 :
  91. minimum(list)
  92. # for break program after run all choices
  93. elif choice == 6 :
  94. #factorial(20)
  95. print("{} of 20 is: {}".format(stringArray[0], answerArray[0]))
  96. #median(list)
  97. print("{} is: {}".format(stringArray[1], answerArray[1]))
  98. #average(list)
  99. print("{} is: {}".format(stringArray[2], answerArray[2]))
  100. #maximum(list)
  101. print("{} is: {}".format(stringArray[3], answerArray[3]))
  102. #minimum(list)
  103. print("{} is: {}".format(stringArray[4], answerArray[4]))
  104. break;
  105. # enter valid choice
  106. else:
  107. print("please enter valid choice")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement