In: Computer Science
Use Python to write the following code.
Program Specifications:
You are to design the following menu:
G] Get a number
S] Display current sum
A] Display current average
H] Display the current highest number
L] Display the current lowest number
D] Display all numbers entered
Q] Quit
If the user enters G, S, A, H, L, or D before selecting G the program needs to advise the user to go and enter a number first.
Rules for the Programmer (you)
Code:
input_list=[] #declare input list
def get_number(): #get_number() function which takes input and append to list
val=int(input("\nEnter an element: "))
input_list.append(val)
main() #calling main() after taking input
def display_sum(): #function to display sum
if len(input_list)>0: #if input contains elements then
display sum
print("\nCurrent sum is {}".format(sum(input_list)))
else:
print("\nNo numbers in list.")
main() #calling main() for displaying menu
def display_avg(): #function display average
if len(input_list)>0:
print("\nCurrent average is
{}".format(sum(input_list)/len(input_list))) #printing average by
finding sum / length of list
else:
print("\nNo numbers in list.")
main()
def display_max(): #function to display max
if len(input_list)>0:
print("\nCurrent highest number is {}".format(max(input_list)))
else:
print("\nNo numbers in list.")
main()
def display_min(): #function to display minimum of input list
if len(input_list)>0:
print("\nCurrent lowest number is {}".format(min(input_list)))
#printing min value
else:
print("\nNo numbers in list.")
main() #calling main() function
def display_all(): #display all values in list function
if len(input_list)>0:
print("\nAll numbers are {}".format(input_list)) #if list contains
elements display values
else:
print("\nNo numbers in list.")
main()
def quit_program(): #function to quit the program
return
def main():
print("\nG] Get a number\nS] Display current sum\nA] Display
current average")
print("H] Display the current highest number\nL] Display the
current lowest number")
print("D] Display all numbers entered\nQ] Quit") #print menu
option=input("Enter an option: ") #take option from user
if option=='G': #based on option call the appropriate
function
get_number()
elif option=='S':
display_sum()
elif option=='A':
display_avg()
elif option=='H':
display_max()
elif option=='L':
display_min()
elif option=='D':
display_all()
elif option=='Q':
quit_program()
else:
print("Invalid option. Select valid option") #any option out of
menu print invalid option
main()
main()
Code and Output Screenshots:
Output:
Note: In python indentation is very important so please refer the attached code screenshots for better and perfect indendation.
Note: if you have any queries please post a comment before giving any reviews thanks a lot..always available to help you...