Link Search Menu Expand Document

CLI Program Execution

Table of contents

  1. Description
  2. addition.py
    1. Navigate to directory and execute:
  3. addition-cli.py
    1. Navigate to directory and execute:

Description

You can easily execute your Python programs directly from the command line. First, navigate to the directory the .py file is stored. Then, use the python3 command to execute the file.

addition.py

The following program prompts the user for two integers using the input() function, then displays the sum. It is saved in the /Users/bianca/test directory.

def main():
    try:
        number1 = int(input('Enter 1st number: '))
        number2 = int(input('Enter 2nd number: '))
    except ValueError:
        print('\nYou must enter two integer arguments. Ending Execution')
        return
    
    print(f'\nThe sum is {number1 + number2}')

if __name__ == '__main__':
    main()

addition-cli.py

The following program accepts two integer arguments from the command line, then displays the sum. It is saved in the /Users/bianca/test directory.

import sys

def main():
    try:
        number1 = int(sys.argv[1])
        number2 = int(sys.argv[2])
    except ValueError:
        print('\nYou must enter two integer arguments. Ending Execution')
        return
    
    print(f'\nThe sum is {number1 + number2}')

if __name__ == '__main__':
    main()