145k views
3 votes
Write a program that prompts a user to enter numeric values. As each value is entered, the program should: - Append the value to a list - Display the values of the list - Display the updated average of the list - Continue to prompt until the user enters any of: quit, stop, or end. Allow upper or lower case

User Evan Byrne
by
8.2k points

1 Answer

0 votes

Answer:

Following are the program in the Python Programming Language:

my_lst=[] #set list

while(True): #set while loop

print("Enter stop, end or quit to stop the loop")

n=input("Enter the number of elements: ")

#get input from user

if(n=="quit" or n=="QUIT" or n=="stop" or n=="STOP" or n=="end" or n=="END"): #set if statement for break the loop

print(n)

break

else: #set else statement

a=int(n) #type conversion

for num in range(0,a): #set for loop

l=float(input("Enter the element for list: ")) #get elements of list

my_lst.append(l) #elements append to the list

print("List: ",my_lst) #print list

print("Average of the list:", sum(my_lst)/a)

#print average of the list

Output:

Enter the number of elements: 5

Enter the element for list: 1

Enter the element for list: 2

Enter the element for list: 3

Enter the element for list: 4

Enter the element for list: 5

List: [1.0, 2.0, 3.0, 4.0, 5.0]

Average of the list: 3.0

Enter the number of elements: STOP

STOP

Step-by-step explanation:

Here, we set list data type variable "my_lst" then, we set the while loop statement and pass condition "True" and inside it.

  • Then, we set a variable "n" and get input from the user in it.
  • Then, we set the if statement and pass the condition is when user input stop, end, or quit then loop is break.
  • Then, otherwise we set the variable "a" and the value of "n" is converted into the integer and store into "a".
  • Then, we set the for loop statement inside the else statement which starts from 0 to n inside the loop.
  • Then, we set the variable "l" which stores the elements of the list from the user in float data type.
  • Then, we append the elements of the "l" into the list "my_lst" and end for loop.
  • Finally, we print the list with message and then print average with message.
User Letta
by
8.3k points