Part of the series

Several example codes

~1 min read • Updated Oct 7, 2025

Program Overview

This Python program reads numbers from the user one by one.
It keeps adding them to a running total as long as each new number is greater than or equal to the previous one.
If a number is entered that is smaller than the previous number, the program stops and displays the final sum.


Python Code:


total = 0
prev = None

while True:
    num = float(input("Enter a number: "))
    if prev is not None and num < prev:
        break
    total += num
    prev = num

print(f"Total sum: {total}")

Sample Output:


Enter a number: 5  
Enter a number: 7  
Enter a number: 10  
Enter a number: 6  

Total sum: 22.0

Step-by-Step Explanation:

- The variable total stores the running sum
- prev keeps track of the previous number entered
- Each new number is compared to the previous one
- If the new number is smaller, the loop breaks
- Otherwise, the number is added to the total and stored as prev


Written & researched by Dr. Shahin Siami