Adding Values - Python Programming Exercise

In this exercise, you will develop a Python program that prompts the user for an undetermined amount of numbers until the user enters 0. The program will then display the sum of the numbers entered. This task introduces you to the use of a while loop for creating dynamic programs that handle user input until a specific exit condition (in this case, entering 0) is met. By completing this exercise, you will enhance your skills in implementing repetitive processes and managing user input in Python. This type of problem-solving is essential for building interactive and flexible Python programs that can adapt to varying amounts of input and calculate dynamic results like a sum.

 Category

Mastering Flow Control

 Exercise

Adding Values

 Objective

Develop a Python program that prompts the user for an undetermined amount of numbers (until 0 is entered) and displays their sum, as follows:

Number? 5
Total = 5
Number? 10
Total = 15
Number? -2
Total = 13
Number? 0
Finished

 Example Python Exercise

 Copy Python Code
# Initialize the total sum variable
total = 0

# Use a while loop to prompt the user for numbers until 0 is entered
while True:
    num = float(input("Number? "))
    
    # Check if the entered number is 0
    if num == 0:
        print("Finished")
        break
    
    # Add the entered number to the total sum
    total += num
    
    # Display the current total sum
    print(f"Total = {total}")

 Output

Number? 5
Total = 5
Number? 10
Total = 15
Number? -2
Total = 13
Number? 0
Finished

 Share this Python Exercise

 More Python Programming Exercises of Mastering Flow Control

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.

  •  Pair of Negative Values

    In this exercise, you will develop a Python program that prompts the user for two numbers and checks if both of them are negative. This task will help y...

  •  Single or Pair of Negative Values

    In this exercise, you will develop a Python program that prompts the user for two numbers and checks whether both are negative, only one is negative...

  •  Factors and Multiples

    In this exercise, you will develop a Python program that displays the numbers from 1 to 500 that are multiples of both 3 and 5 on the screen. You...

  •  Repeated Value

    In this exercise, you will develop a Python program that prompts the user for a number and a quantity, then displays that number repeated as many...

  •  Access Code

    In this exercise, you will develop a Python program that prompts the user to enter their login and password (both must be integer numbers). The p...

  •  Access Code V2

    In this exercise, you will develop a Python program that prompts the user for their login and password, both of which must be integers. The progr...