Write a python program that 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 and average of the numbers. Use while loop and if statements for this assignment.
Please show pseudocode, flow chart and screenshot of the output.
Here is the code to do so:
Pseudo code:
step 1: Take input from user
Step 2: if the input is the enter key then calculate average and sum
Step 3: if the input was not enter add the entry in the variable called sum and increase the count of numbers by 1
The codes are well commented and easy to understand, if the answer helped you please upvote and if you have any doubts please comment i will surely help. please take care of the indentation while copying the code. Check from the screenshots provided.
Code:
# initialise the necessary variables
avg = 0.0
total = 0.0
numbers = []
while True:
data = input("Enter a number or press Enter to quit: ")
# if a enter was pressed show the output and break the loop
if data == "":
total = sum(numbers)
avg = total/len(numbers)
print(f"The sum is {total},")
print(f"The average is {avg}.")
break
# else append the data to a list.
else:
numbers.append(float(data))
continue
Get Answers For Free
Most questions answered within 1 hours.