write a python program take 2 num from the user and print their sum pass all the test cases

Python program to take two numbers from user input and print their sum with test cases 2025

This refined query specifies the programming language (Python), clarifies the task (taking two numbers from user input and printing their sum), and includes 'test cases' to target resources that validate the solution, while also indicating the current year for relevance.

To create a Python program that takes two numbers from the user and prints their sum, we will leverage the built-in input() function to capture user input, convert that input into integers (or floats, based on your requirement), and then compute the sum. Below is a complete program along with example test cases to ensure functionality.

Complete Python Program

# Function to add two numbers
def add_two_numbers(num1, num2):
    return num1 + num2

# Main function to execute the program
def main():
    try:
        # Taking input from the user
        number1 = float(input("Enter the first number: "))  # Using float for broader number types
        number2 = float(input("Enter the second number: "))
        
        # Calculating the sum
        result = add_two_numbers(number1, number2)
        
        # Printing the sum
        print(f"The sum of {number1} and {number2} is: {result}")
        
    except ValueError:
        print("Please enter valid numbers.")

# Calling the main function
if __name__ == "__main__":
    main()

Explanation of the Code

  1. Function Definition:

    • The function add_two_numbers(num1, num2) takes two parameters and returns their sum.
  2. User Input:

    • The main() function prompts the user to enter two numbers. We use float(input()) to ensure that both integer and decimal inputs are valid.
  3. Error Handling:

    • A try-except block catches any ValueError exceptions, which might occur if the user enters non-numeric values.
  4. Output:

    • Finally, the program prints the results in a user-friendly format.

Test Cases

To validate the functionality of the program, you can run the following test cases:

  1. Test Case 1:

    • Input: 3 and 5
    • Output: The sum of 3.0 and 5.0 is: 8.0
  2. Test Case 2:

    • Input: -2.5 and 1.5
    • Output: The sum of -2.5 and 1.5 is: -1.0
  3. Test Case 3:

    • Input: 0 and 0
    • Output: The sum of 0.0 and 0.0 is: 0.0
  4. Test Case 4 (invalid input):

    • Input: abc and 2
    • Output: Please enter valid numbers.

Conclusion

This program efficiently captures user input, performs a basic arithmetic operation, and handles errors gracefully. By running the suggested test cases, you can confirm that the program behaves as expected under various scenarios. Feel free to modify the prompt messages or the data types as per your specific requirements.

Related Searches

Sources

10
1
How to Add Two Numbers in Python - GeeksforGeeks
Geeksforgeeks

The task of adding two numbers in Python involves taking two input values and computing their sum using various techniques .

2
how to print the sum of all the inputs? - python - Stack Overflow
Stack Overflow

I wanted to take two numbers as an input 5 times and then print out all their sum. ... How can i get sum of all the numbers that this program ...

3
How to Add Two Numbers in Python - W3Schools
W3schools

Add Two Numbers with User Input. In this example, the user must input two numbers. Then we print the sum by calculating (adding) the two numbers: Example. x ...

4
How to write a Python program that asks the user to enter two ...
Quora

Write a program that allows the user to enter two numbers and then will output the sum of the two numbers.

5
How to add two numbers from input() - Python Discussions
Discuss

Modify the following code snippet to add two numbers. For example: Test Input Result get_input() 12 Enter number one: 12

6
Python Program To Add Two Numbers - Flexiple
Flexiple

This blog post will guide you through creating a straightforward Python script that performs this basic mathematical operation.

7
Can you write a program in Python that takes in two numbers and ...
Quora

How do I write a Python program that asks the user to enter two numbers and print their sum? Python programs are just plain text files, and ...

8
Python's sum(): The Pythonic Way to Sum Values
Realpython

In this step-by-step tutorial, you'll learn how to use Python's sum() function to add numeric values together. You also learn how to concatenate sequences, ...

9
Input and Output in Python - GeeksforGeeks
Geeksforgeeks

Print Numbers in Python. The code prompts the user to input an integer representing the number of roses, converts the input to an integer ...

10
Addition of multiple numbers in python | Sololearn: Learn to code for ...
Sololearn

I wrote a code like this def add(*args): sum = 0 for i in args: sum = sum + i return sum print(add(3,4)) I directly given values in the add function call.