write me a python script to add two numbers
- User
write me a python script to add two numbers
- Assistant
python# Simple Python script to add two numbers
def add_two_numbers(a, b): """ Function to add two numbers.
Parameters: a (int or float): First number b (int or float): Second number Returns: int or float: Sum of a and b """ return a + bGet input from user
try: num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: "))
result = add_two_numbers(num1, num2) print(f"The sum of {num1} and {num2} is: {result}")except ValueError: print("Error: Please enter valid numbers.") How to use:
Save this code to a file (e.g., add_numbers.py) Run it with Python: python add_numbers.py Enter two numbers when prompted
Example output: textEnter the first number: 5 Enter the second number: 3 The sum of 5.0 and 3.0 is: 8.0 The script includes error handling for invalid inputs and works with both integers and decimal numbers.python# Simple Python script to add two numbers