SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Repetition Structures
Exercise:
Algorithm Workbench
Question:4 | ISBN:9780132576376 | Edition: 2

Question

Write a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#Initialized the variable
sum = 0
#Prompt and read 10 numbers from the user
for _ in range(10):
    number = float(input("Enter a number: "))
    #Calculate the total of the numbers entered
    sum += number
#Display output
print("The total is:", sum)

Executed Output 1:

Enter a number: 5
Enter a number: 10
Enter a number: -10
Enter a number: -5
Enter a number: 20
Enter a number: 30
Enter a number: -50
Enter a number: 20
Enter a number: 60
Enter a number: -45
The total is: 35.0

Executed Output 2:

Enter a number: 10
Enter a number: 20
Enter a number: 30
Enter a number: 40
Enter a number: 50
Enter a number: 5
Enter a number: 4
Enter a number: 3
Enter a number: 2
Enter a number: 1
The total is: 165.0

 

0 0

Discussions

Post the discussion to improve the above solution.