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.
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}")
Enter a number: 5
Enter a number: 7
Enter a number: 10
Enter a number: 6
Total sum: 22.0
- 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