Write a Python program to do the following: (a)Ask the user to enter as many integers from 1 to 10 as he/she wants. Store the integers entered by the user in a list. Every time after the user has entered an integer, use a yes/no type question to ask whether he/she wants to enter another one. (b)Display the list. (c)Calculate and display the average of the integers in the list. (d)If the average is higher than 7, subtract 1 from every number in the list. Display the modified list.

Respuesta :

Answer:

// program in Python

#empty list to store user input

i_list = []

#part a

#Loop until we read all the integer inputs from the user

while True:

   #read input from user

   value = input("Enter an integer (1-10): ")

   i_list.append(int(value))

   #read choice

   choice = input("Enter again? [y/n]: ")

   if choice == 'n':

       break

 

#part b

#Print list

print("Number List: " + str(i_list))

#part c

#find sum and Average

sum = 0

for i in i_list:

   #sum of all elements

   sum += i

#Average

avg = sum / len(i_list);

#print Average

print("Average: " + str(avg))

#part d

#If Avg > 7, subtract 1 from each element in the list and then display

if avg > 7:

   for i in range(0, len(i_list)):

       #subtract 1 from each element

       i_list[i] = i_list[i] - 1

#print new list

print("Modified List: " + str(i_list))  

Explanation:

Create an empty list to store the user input.Read input number from user  and store it into list.Then ask for choice(y/n).If user's choice is 'y' then again read a number and store it into list.Repeat this until user's  choice is 'n'. Find the sum and average of all inputs numbers and print the list,  and their average.If the average is greater than 7, subtract 1 from all the  inputs of the list and print Modified list.

Output:

Enter an integer (1-10): 9                                                                                                

Enter again? [y/n]: y                                                                                                      

Enter an integer (1-10): 6                                                                                                

Enter again? [y/n]: y                                                                                                      

Enter an integer (1-10): 7                                                                                                

Enter again? [y/n]: y                                                                                                      

Enter an integer (1-10): 8                                                                                                

Enter again? [y/n]: n                                                                                                      

Number List: [9, 6, 7, 8]                                                                                                  

Average: 7.5                                                                                                              

Modified List: [8, 5, 6, 7]