Practice

Accepting User Input with Args

To accept basic arguments from the command line, we can use sys.argv. Start a new Python file called cli_exercise.py and enter the following.

import sys

args = sys.argv

print(args)

Then, run it from the command line:

(env) $ python cli_exercise.py
Here's what you should have seen on your command line:

You should see a list with one item: the name of your program. Pass in additional arguments by adding them after your program name on the command line, separated by spaces:

(env) $ python cli_exercise.py argument1 argument2 "hello world"
Here's what you should have seen on your command line:

Note that the name of the file you’re running is rarely useful, so it’s common to see this omitted with using slices, for example sys.argv[1:]

Accepting User Input with input

You can also accept user data inside a running program by using input(). Let’s make a simple interactive command line program that asks for a user’s name and birthday. Call it cli_exercise_input.py. Use input() to get the user’s name and birthday, and greet the user (call strip() on their name to remove any extra whitespace).

You should have written something like this:
Here's what you should have seen on your command line:

Optional Advanced Exercise

If you thought this exercise was a breeze, try this optional advanced exercises.

Refactor the final exercise from Intro to Python, using custom exceptions and a class to store the information about a GitHub Repo. Accept the list of languages as user input.

You can see an example implementation in the repo for this course at git.io/python3