Numerous Numbers and Their Sum - Python Programming Exercise

This Python program prompts the user to enter several numbers, calculating their sum as they go. The program continues to prompt the user for numbers until they enter "end". At that point, it displays all the numbers entered and the total sum of those numbers. The program uses a while loop to repeatedly ask the user for input, checking if the input is the string "end" to stop the loop. It stores the numbers entered in a list and updates the sum after each entry. Once the loop ends, the program prints all the entered numbers and the cumulative sum. This program is a great exercise for understanding loops, conditional logic, and list management in Python. It also practices handling user input and performing accumulation operations such as summing values. The task reinforces the use of string comparison and list operations, along with printing the results in a user-friendly format.

 Category

Arrays, Lists, and Strings

 Exercise

Numerous Numbers And Their Sum

 Objective

Develop a Python program that prompts the user for several numbers (until they enter "end") and displays their sum. When the execution is about to end, it must display all the numbers entered and the sum again, as follows:

Enter a number: 5
Sum = 5
Enter a number: 3
Sum = 8
Enter a number: end
The numbers are: 5 3
The sum is: 8

 Example Python Exercise

 Copy Python Code
# Program developed by: Programmer 1, Programmer 2

# Initialize an empty list to store the numbers
numbers = []
total_sum = 0

# Start prompting the user for numbers
while True:
    number = input("Enter a number: ")
    
    if number.lower() == "end":
        break
    
    # Try to convert the input to a number
    try:
        number = float(number)  # Convert the input to a float
        numbers.append(number)  # Add the number to the list
        total_sum += number  # Add the number to the sum
        print(f"Sum = {total_sum}")  # Display the current sum
    except ValueError:
        print("Please enter a valid number or 'end' to stop.")  # Handle invalid input

# Display all the numbers entered and the final sum
print("The numbers are:", ' '.join(map(str, numbers)))
print(f"The sum is: {total_sum}")

 Output

Enter a number: 5
Sum = 5
Enter a number: 3
Sum = 8
Enter a number: 7
Sum = 15
Enter a number: -2
Sum = 13
Enter a number: 4.5
Sum = 17.5
Enter a number: end
The numbers are: 5.0 3.0 7.0 -2.0 4.5
The sum is: 17.5

 Share this Python Exercise

 More Python Programming Exercises of Arrays, Lists, and Strings

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Matrix Array

    This Python programming exercise is ideal for practicing the handling of multidimensional arrays. In this case, the goal is to prompt the user for the marks of 20 students, ...

  •  Advanced Statistics

    This Python statistical program is a fantastic exercise for improving data handling and user interaction in programming. The program allows users to add new data...

  •  Namedtuple

    This Python program is an excellent introduction to using NamedTuples for storing structured data. The program allows users to define 2D points with specific f...

  •  Array of Namedtuple

    In this exercise, you will develop a Python program that expands the previous exercise (NamedTuple point), allowing up to 1,000 points to be stored using an "array of...

  •  Array of Namedtuple and menu

    In this exercise, you will develop a Python program that expands the previous exercise (array of NamedTuples), so that it displays a menu where the user...

  •  Library Database

    In this exercise, you will develop a Python program to create a small database for storing book data. For each book, you will keep the following information: Title...