Calculating an Unlimited Sum - Python Programming Exercise

In this exercise, you will develop a Python program to calculate an unlimited sum by continuously adding numbers provided by the user. This exercise is perfect for practicing user input handling, loops, and basic arithmetic operations in Python. By implementing this program, you will gain hands-on experience in handling user input, loops, and arithmetic operations in Python. This exercise not only reinforces your understanding of user input handling but also helps you develop efficient coding practices for managing user interactions.

 Category

Memory Management Techniques

 Exercise

Calculating An Unlimited Sum

 Objective

Develop a Python program to calculate an unlimited sum by continuously adding numbers provided by the user. The program should allow the user to input numbers indefinitely, and keep a running total. The user should be able to stop the input process at any time (e.g., by entering 'exit' or a special keyword), and the program should display the final sum.

 Example Python Exercise

 Copy Python Code
def calculate_sum():
    """Continuously adds numbers provided by the user and keeps a running total."""
    total = 0  # Initialize the running total to 0
    
    print("Enter numbers to add to the sum. Type 'exit' to stop and display the final sum.")
    
    while True:
        user_input = input("Enter a number: ").strip()  # Take user input and remove extra spaces
        
        if user_input.lower() == 'exit':
            # If the user types 'exit', stop the loop and display the final sum
            print(f"The final sum is: {total}")
            break
        
        try:
            # Try to convert the input to a float and add it to the total
            number = float(user_input)
            total += number
        except ValueError:
            # If the input is not a valid number, print an error message
            print("Invalid input. Please enter a valid number or type 'exit' to stop.")
            
def main():
    """Main function to run the sum calculation program."""
    calculate_sum()

# Run the program
if __name__ == "__main__":
    main()

 Output

Enter numbers to add to the sum. Type 'exit' to stop and display the final sum.
Enter a number: 10
Enter a number: 25.5
Enter a number: -5
Enter a number: exit
The final sum is: 30.5

 Share this Python Exercise

 More Python Programming Exercises of Memory Management Techniques

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.

  •  ArrayList - Reading a Text File

    In this exercise, you will develop a Python program that uses an ArrayList-like structure (a list) to read and store the contents of a text file. This exercise...

  •  Hash Table - Implementing a Dictionary

    In this exercise, you will develop a Python program that implements a hash table using a dictionary. This exercise is perfect for practicing data structures, d...

  •  Parenthesis Matching

    In this exercise, you will develop a Python program that checks if parentheses in a given expression are properly balanced. This exercise is perfect for practi...

  •  Merging and Sorting Files

    In this exercise, you will develop a Python program that merges the contents of multiple text files into a single file and sorts the content alphabetically or numeric...

  •  ArrayList: Storing Points

    In this exercise, you will develop a Python program that uses an ArrayList-like structure (a list) to store a collection of points, where each point is represented by...

  •  File Search Operations

    In this exercise, you will develop a Python program that searches for a specific word or phrase within a text file. This exercise is perfect for practicing fil...