Unable to have a command line parameter in Python

I run

import sys

print "x \tx^3\tx^3+x^3\t(x+1)^3\tcube+cube=cube+1"

for i in range(sys.argv[2]):              // mistake here
    cube=i*i*i
    cube2=cube+cube
    cube3=(i+1)*(i+1)*(i+1)
    truth=(cube2==cube3)

    print i, "\t", cube, "\t", cube + cube, "\t", cube3, "\t", truth

I get

Traceback (most recent call last):
  File "cube.py", line 5, in <module>
    for i in range(sys.argv[2]):
IndexError: list index out of range

How can you use command line parameter as follows in the code?

Example of the use

python cube.py 100

It should give

x   x^3 x^3+x^3 (x+1)^3 cube+cube=cube+1
0   0   0   1   False
1   1   2   8   False
2   8   16  27  False
--- cut ---
97  912673  1825346     941192  False
98  941192  1882384     970299  False
99  970299  1940598     1000000     False

Upvotes: 0

Views: 402

Answers (5)

he1ix
he1ix

Reputation: 380

I'd like to suggest having a look at Python's argparse module, which is a giant improvement in parsing commandline parameters - it can also do the conversion to int for you including type-checking and error-reporting / generation of help messages.

Upvotes: 1

John Machin
John Machin

Reputation: 82924

Here are some tips on how you can often solve this type of problem yourself:

Read what the error message is telling you: "list index out of range".

What list? Two choices (1) the list returned by range (2) sys.argv

In this case, it can't be (1); it's impossible to get that error out of for i in range(some_integer) ... but you may not know that, so in general, if there are multiple choices within a line for the source of an error, and you can't see which is the cause, split the line into two or more statements:

num_things = sys.argv[2]
for i in range(num_things):

and run the code again.

By now we know that sys.argv is the list. What index? Must be 2. How come that's out of range? Knowledge-based answer: Because Python counts list indexes from 0. Experiment-based answer: Insert this line before the failing line:

print list(enumerate(sys.argv))

So you need to change the [2] to [1]. Then you will get another error, because in range(n) the n must be an integer, not a string ... and you can work through this new problem in a similar fashion -- extra tip: look up range() in the docs.

Upvotes: 2

nosklo
nosklo

Reputation: 222802

Use:

sys.argv[1]

also note that arguments are always strings, and range expects an integer.

So the correct code would be:

for i in range(int(sys.argv[1])):

Upvotes: 6

Cdsboy
Cdsboy

Reputation: 730

Its sys.argv[1] instead of 2. You also want to makes sure that you convert that to an integer if you're doing math with it.

so instead of

for i in range(sys.argv[2]):

you want

for i in range(int(sys.argv[1])):

Upvotes: 1

FogleBird
FogleBird

Reputation: 76762

You want int(sys.argv[1]) not 2.

Ideally you would check the length of sys.argv first and print a useful error message if the user doesn't provide the proper arguments.

Edit: See http://www.faqs.org/docs/diveintopython/kgp_commandline.html

Upvotes: 3

Related Questions