tags:

views:

88

answers:

2

When running the program below from a command prompt in windows it keeps displaying the 'IndexError: list Index out of range', even when I am definitely adding a number argument eg.'bigdigits.py 123'

The program runs fine if I assign a string to 'digits' manually in the program eg. digits = '123'.

import sys

Zero = [" *** ", "*   *", "*   *", "*   *", "*   *", "*   *", " *** "]  
One = [" **  ", "  *  ", "  *  ", "  *  ", "  *  ", "  *  ", " *** "]  
Two = [" **   ", "*  * ", "   * ", "  *  ", " *   ", "*    ", "**** "]  
Three = ["*****", "   * ", "  *  ", " *** ", "    *", "*   *", " *** "]  
Four = ["  *  ", " **  ", "* *  ", "*****", "  *  ", "  *  ", "  *  "]  
Five = ["*****", "*    ", "***  ", "   * ", "    *", "*   *", " *** "]  
Six = ["  ** ", " *   ", "*    ", "***  ", "*  * ", "*  * ", " **  "]  
Seven = ["*****", "    *", "   * ", "  *  ", " *   ", " *   ", " *   "]  
Eight = [" *** ", "*   *", "*   *", " *** ", "*   *", "*   *", " *** "]  
Nine = [" *** ", "*   *", "*   *", " *** ", "  *  ", " *   ", "*    "]  

Digits = [Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine]

try:  
    digits = sys.argv[1]  
    row = 0  
    while row < 7:  
        line = ""  
        column = 0  
        while column < len(digits):  
            number = int(digits[column])  
            digit = Digits[number]  
            line += digit[row] + " "  
            column += 1  
        print(line)  
        row += 1  

except ValueError as err:  
    print(err, "in", digits) 
A: 

This is not an answer, because I can't reproduce your problem. I just thought I'd rewrite your loops a bit more Pythonically...

try:  
    digits = sys.argv[1]
    for row in range( 7 ):
        for column in digits:
            print( Digits[ int( column ) ][ row ], end=" " )
        print( )

except ValueError as err:  
    print( err, "in", digits )

A general rule of thumb is that if you want to iterate over the elements of some container, use a for loop. while loops are more-or-less restricted to the case of waiting for something to occur.

katrielalex
A: 

Remove the try/except stuff. Why ValueError???? Just let it crash (if it crashes) and get a proper traceback that will show you which line is causing what problem. Note that there are 4 lines which do list indexing; at the moment, if you had IndexError in the except, you couldn't tell which without very careful examination of the code.

By the way: untidy code conceals bugs, like the top row of the "2" being 6 pixels wide instead of 5. Suggested layout:

Two = [
    " **   ", # <<<=== Whoops!
    "*  * ",
    "   * ",
    "  *  ",
    " *   ",
    "*    ",
    "**** "
    ]  
John Machin
Thanks for your comments guys, I am a total newbee, I got it working, I wasnt entering 'python' on the command line before the program !! doh!
Paris