Main Function Parameters and Summation - Python Programming Exercise

In this exercise, you will develop a Python program named "sum" that takes two integer numbers from the command line and prints their sum. This exercise is perfect for practicing command-line input handling and arithmetic operations in Python. By implementing this program, you will gain hands-on experience in handling command-line input and performing arithmetic operations in Python. This exercise not only reinforces your understanding of command-line input handling but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Main Function Parameters And Summation

 Objective

Develop a Python program named "sum" that takes two integer numbers from the command line and prints their sum. For example, if you run the program with the command sum 5 3, it should output 8.

 Example Python Exercise

 Copy Python Code
import sys

# Define the sum function
def sum_numbers(a, b):
    return a + b

# Main function to handle command line input
def main():
    # Check if we have exactly two arguments (excluding the script name)
    if len(sys.argv) != 3:
        print("Please provide exactly two numbers.")
        return

    try:
        # Parse the command line arguments as integers
        num1 = int(sys.argv[1])
        num2 = int(sys.argv[2])

        # Calculate and print the sum
        result = sum_numbers(num1, num2)
        print(result)

    except ValueError:
        # Handle the case where the inputs are not valid integers
        print("Please provide valid integer numbers.")

# Call the main function to execute the program
if __name__ == "__main__":
    main()

 Output

python sum.py 5 3

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

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.

  •  Function to Calculate Sum of Digits

    In this exercise, you will develop a Python program with a function named "sum_digits" that takes a number as input and returns the sum of its digits. This exercis...

  •  Function to Compute Factorial

    In this exercise, you will develop a Python program with a recursive function to compute the factorial of a given number. The factorial of a number is defined as: 𝑛...

  •  Main Function Parameters and Reversal

    In this exercise, you will develop a Python program named "reverse" that takes multiple words from the command line and prints them in reverse order. This exercise...

  •  Function to Obtain Integer Value

    In this exercise, you will develop a Python program with a function named "get_int" that displays the text received as a parameter, prompts the user for an integer, a...

  •  Function to Manage Task Database

    In this exercise, you will develop a Python program to enhance the "tasks database" by dividing it into multiple functions for better organization and readability. Th...

  •  Function to Find Maximum Value in an Array

    In this exercise, you will develop a Python program containing a function that accepts a list of floating-point numbers as input and returns the maximum value within ...