Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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.
- #Joseph Shaw
- #Menu driven program
- # create a list with given values
- list = [3,4,84,5,2,47,7]
- # sorting of list
- list.sort()
- #print choices
- print("1. factorial \n2. median \n3. average \n4. maximum \n5. minimum \n6. exit\n")
- #create stringArray for functions
- stringArray = []
- #create answersArray for answers
- answerArray = []
- # function to calculate factorial of 20
- def factorial(n):
- fact = 1
- # calculate factorial of 20
- for i in range (1,21):
- fact = fact * i
- # print factorial
- print("factorial is : ",fact)
- stringArray.insert(0, 'Factorial')
- answerArray.insert(0, fact)
- #print(stringArray[0])
- #print(answerArray[0])
- # function of find median of list (middle value in array)
- def median(list):
- # find median of list
- median = list[(len(list)-1)//2]
- #print median
- print("median is : ",median)
- stringArray.insert(1, 'Median')
- answerArray.insert(1, median)
- # function to calculate average
- def average(list):
- # sum(answerArray) for sum of list and len(answerArray) for length of list
- avg = sum(list)/len(list)
- #print average
- print("average is : ",avg)
- stringArray.insert(2, 'Average')
- answerArray.insert(2, avg)
- # function to find maximum number in list (last element in sorted list)
- def maximum(list):
- # find maximum number in list
- max = list[len(list)-1]
- # print maximum number
- print("maximum is : ",max)
- stringArray.insert(3, 'Maximum')
- answerArray.insert(3, max)
- # function to find minimum number in list (first element in sorted list)
- def minimum(list):
- # find minimum number in list
- min = list[0]
- # print minimum number
- print("minimum is : ",min)
- stringArray.insert(4, 'Minimum')
- answerArray.insert(4, min)
- while True:
- # enter choice
- choice = int(input("Enter your choice \n"))
- # for factorial of 20
- if choice == 1:
- n=20
- factorial(n)
- # for median of list
- elif choice == 2 :
- median(list)
- # for average of list
- elif choice == 3 :
- average(list)
- # for maximum of list
- elif choice == 4 :
- maximum(list)
- # for minimum of list
- elif choice == 5 :
- minimum(list)
- # for break program after run all choices
- elif choice == 6 :
- #factorial(20)
- print("{} of 20 is: {}".format(stringArray[0], answerArray[0]))
- #median(list)
- print("{} is: {}".format(stringArray[1], answerArray[1]))
- #average(list)
- print("{} is: {}".format(stringArray[2], answerArray[2]))
- #maximum(list)
- print("{} is: {}".format(stringArray[3], answerArray[3]))
- #minimum(list)
- print("{} is: {}".format(stringArray[4], answerArray[4]))
- break;
- # enter valid choice
- else:
- print("please enter valid choice")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement