Arguments are important for all programs. Arguments for programs have nothing to do with shouting, but are additional bits of information supplied to the program when it is run. Open a new Python script nano and type this;

import sys

for i, argument in enumerate(sys.argv):
    print("Argument %s equals %s" % (i, argument))

Run this script by typing

python here are some arguments

What do you see? Can you work out what happened?

In this case you passed four arguments to your script; here, are, some and arguments. The Python interpreter read those arguments and placed them, together with the name of the script, into a special variable called sys.argv that you can access from your script (the sys.argv variable comes from the module called sys. This is why we have to load (import) the sys module at the start of the script using the line import sys).


Use the knowledge you’ve gained so far to write a Python script that can print out any times table. Call your script, and have it read two arguments. The first argument should be the times table to print (e.g. the five times table) while the second should be the highest value of the times table to go up to. So

python 5 12

should print the five times table from 1 times 5 to 12 times 5.

Note that the arguments are loaded into Python as strings. You will need to convert them to integers by using the lines like;

n = int(sys.argv[1])

Answer (don’t peek at this unless you are stuck or until you have finished!)

As an extension, can you think of a way to use lists to print out the times table using words rather than using numbers? To do this you will need to know that you can assign values to a list using the following syntax;

a = [1, 2, 3, 4, 5]
b = ["cat", "dog", "fish", "bird"]
c = ["zero", "one", "two", "three"]

Answer (don’t peek at this unless you are stuck or until you have finished!)

Previous Up Next