Edit the program provided so that it receives a series of numbers from the user and allows the user to press the enter key to indicate that he or she is finished providing inputs. After the user presses the enter key, the program should print: The sum of the numbers The average of the numbers

Respuesta :

Answer:

The folllowing are the code to this question:

Sum= 0.0#defining float variable Sum

n = 0# defining integer variable n for count number  

while True:#defining for loop for calculate Sum

   number= input("Enter a number and for exit press Enter: ")#defining number variable for user input

   if number== '':#defining if block that checks number is empty

       break#use break key word

   ad= float(number)#convert the string value into float  

   Sum += ad #add value in sum variable

   n += 1#increment the value of n

print("The sum is: ", Sum)

if n > 0:#use if for calculate average

   avg = Sum / n #calculate average value  

   print('The average is', avg)#use print method to print average value

else:#else block

   print('undefined')#print message undefined

Output:

please find the attached file.

Explanation:

In the above code, the "Sum and n" variable is defined, which is used for calculating the sum and in the next step, a while loop is used in the loop number variable is defined, that input value from the user end and if the block is used, that check last value.

In the loop, the "Sum" variable is used, which adds user input value, and n is used for times of inputs, and outside the loop, the conditional statement is used.

In the if block, it checks count value is greater then 0, if it is true, it will calculate the average and store its value in the "avg" variable, otherwise, it will print 'undefined' as a message.  

ACCESS MORE
EDU ACCESS