


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A guide for students in cs150 course during the fall 2009 semester to practice writing loops that take input from command-line arguments in python. It covers the concept of passing command-line arguments to a python program, the order and data types of arguments, and converting for loops to while loops.
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



In this assignment, you are to practice writing loops that take input from the command line. Up until now, you have learned to run a python program named loop.py like this:
py loop.py
It is possible to pass information to the python program; this technique is called passing command-line arguments. Suppose you wish to pass the values 3, 10, and 2 to the loop program (we will see why we would want to do this further on). You would run the program this way:
py loop.py 3 10 2
In general, whatever extra information we wish to send to our program, we give it after the name of the file containing the program.
Create and move into a directory named clargs. Type in this program, saving it to a file (name the file clargs.py):
import sys
length = len(sys.argv) for i in range(length): print("argument",i,"is",sys.argv[i]) print("the type of arg",i,"is",type(sys.argv[i]))
Run the program from within vim by using the # macro. You should see the following line appear at the bottom of your screen:
:!clear;python3 %
Notice that you don’t see the name clargs.py; you see the symbol % instead. The % symbol is vim shorthand for the name of the file you are currently editing. Type in the numbers 3, 10, and 2, separated by spaces. The line at the bottom of your screen should look like:
:!clear;python3 % 3 10 2
Now press the Enter key. You should see the following output:
argument 0 is clargs.py the type of arg 0 is <class ’str’> argument 1 is 3 the type of arg 1 is <class ’str’> argument 2 is 10 the type of arg 2 is <class ’str’> argument 3 is 2 the type of arg 3 is <class ’str’>
Run the program again, but this time use the command :!!, which repeats your last shell command. Now you can quickly run your python program without having to type in the command line arguments again.
Looking at the output, you should notice three things. First, the arguments include the name of the file you are running and that the counting of these arguments is zero-based. The second thing is that the order of these arguments is the exact same order as presented on the command line. Finally, all the the arguments are strings (even though we passed them as numbers).
This last fact means if we wish to pass a number to a python program, we will need to convert it from a string to a number inside the program. As an example, suppose we wish to add up all the arguments after the name of the program. Our program would look something like this:
import sys
total = 0; length = len(sys.argv) for i in range(1,length): # skip argument zero total += int(sys.argv[i]) print("the total is",total)
Test this program (name it total.py) using the # macro and type in various numbers to see if the program is working correctly.
Finally, enter one last practice program, named for.py:
import sys
start = int(sys.argv[1]) limit = int(sys.argv[2]) step = int(sys.argv[3]) for i in range(start,limit,step): print("i is",i)
If you run this program with the arguments 3, 10, and 2, you should get the following output:
i is 3
(if you were successful).
To submit your activity, make sure you are in the clargs directory. Run the command:
activity cs150 loops