drawing a barcode with turtle graphics on python
Question: I know that there are crazy short cuts to doing a lot of things with Python,
which is where I'm running into trouble with this project for my intro CIS
class. I've searched for variations of my question, but without luck.. SO:

the project is to make our "turtle" draw a barcode using a ZIP that would be
entered into the command line. I have a lot of the structural work done, ie:
encodings for specific digits and telling the turtle how long to draw the bars
for specific digits.. however, now I'm stuck at writing the for loops to
actually put those two pieces together and get the program drawing the
barcode.

here's what I have:

    
    
    import argparse # Used in main program to obtain 5-digit ZIP code from command
                # line
    import time # Used in main program to pause program before exit
    import turtle   # Used in your function to print the bar code
    
    ## Constants used by this program
    SLEEP_TIME = 30 # number of seconds to sleep after drawing the barcode
    ENCODINGS = [[1, 1, 0, 0, 0],   # encoding for '0'
             [0, 0, 0, 1, 1],   # encoding for '1'
             [0, 0, 1, 0, 1],   # encoding for '2'
             [0, 0, 1, 1, 0],   # encoding for '3'
             [0, 1, 0, 0, 1],   # encoding for '4'
             [0, 1, 0, 1, 0],   # encoding for '5'
             [0, 1, 1, 0, 0],   # encoding for '6'
             [1, 0, 0, 0, 1],   # encoding for '7'
             [1, 0, 0, 1, 0],   # encoding for '8'
             [1, 0, 1, 0, 0]    # encoding for '9'
            ]
    SINGLE_LENGTH = 25  # length of a short bar, long bar is twice as long
    
    def compute_check_digit(digits):
    
        sum = 0
        for i in range(len(digits)):
            sum = sum + digits[i]
        check_digit = 10 - (sum % 10)
        if (check_digit == 10):
            check_digit = 0
        return check_digit
    
    
    def draw_bar(my_turtle, digit):
        my_turtle.left(90)
        if digit == 0:
            length = SINGLE_LENGTH
        else:
            length = 2 * SINGLE_LENGTH
        my_turtle.forward(length)
        my_turtle.up()
        my_turtle.backward(length)
        my_turtle.right(90)
        my_turtle.forward(10)
        my_turtle.down()
    
    
    def draw_zip(my_turtle, zip):
        # WHAT DO I DO 
        print("My code to draw the barcode needs to replace this print statement")
    
    def main():
        parser = argparse.ArgumentParser()
        parser.add_argument("ZIP", type=int)
        args = parser.parse_args()
        zip = args.ZIP
        if zip <= 0 or zip > 99999:
            print("zip must be > 0 and < 100000; you provided", zip)
        else:
            my_turtle = turtle.Turtle()
            draw_zip(my_turtle, zip)
            time.sleep(SLEEP_TIME)
    
    if __name__ == "__main__":
        main()
    

The argparse/parser stuff at the beginning and end is given to us when we
start each project.

I know this next line would be helpful somewhere, i looked up the map feature
and i know that i need to convert the encodings to integers from strings.

map(list, str(zip))

Thanks!


Answer: You need to iterate through the digits of the zip. For each digit, you iterate
through the 5 bars.

    
    
    for str_digit in str(zip):
        digit = int(str_digit)
        for bar_bit in ENCODINGS[digit]:
            draw_bar(my_turtle, bar_bit)
            <move turtle to next bar's starting point>
    

I hope this is understandable to you. You can shorten the code with various
Python techniques, but this is easy to understand.

