Python Forum

Full Version: How would I go about repeating/looping this code?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So basically what I have to do, is in a 6x7 grid, from a randomly generated list of instructions such as ['a',1] or ['b',4] draw an object in the grid
('a' or 'b' being the row and 1-4 being which object to draw)
This is the code I have to draw in just one of the tiles in the grid, which so far works for what I want it to do, and obviously if I did this manually for every row I'd end up with thousands of lines of code.
 def play_game(moves):
    if moves[0] == ['a',1] or ['a',2] or ['a',3] or ['a',4]:
        goto (-300,row_a_y_cord[0])
        del row_a_y_cord[0]
        if moves[0] == ['a',1]:
         dot(10, "red")
        elif moves[0] == ['a',2]:
         dot(10, "blue")
        elif moves[0] == ['a',3]:
         dot(10, "yellow")
        elif moves[0] ==['a',4]:
         dot(10, "green")
So I was wondering if there was any way I could set this up better, or loop this for the 40 instructions, or am I just on the wrong track with the way I have coded this?
You could add a parama "loop" which indicates how often to loop.

then you could add a counter and use:
counter=0
while(counter < loop):
    counter+=1
Or did I missunderstood the point of your question ?
Just to try make it a bit clearer, what I would like to do is each loop the index number for moves would increase by 1, so right now it's moves[0], so for the next loop I'd want moves[1], for the second instruction in the list. Would this be achieved by what you've proposed?

Sorry if I'm not being very clear, this is rather new to me.
Why don't you try things out and see if you can get it to work?
Not sure, if Im just to retarded what you want to achieve, but here's an other way to increase the index.

def play_game(moves,max_loop):
    for x in range(0,max_loop):
        if moves[x] == ['a',1] or ['a',2] or ['a',3] or ['a',4]:
            goto (-300,row_a_y_cord[0])
            del row_a_y_cord[0]
            if moves[x] == ['a',1]:
                dot(10, "red")
            elif moves[x] == ['a',2]:
                dot(10, "blue")
            elif moves[x] == ['a',3]:
                dot(10, "yellow")
            elif moves[x] ==['a',4]:
                dot(10, "green")


play_game(moves, 10)
starting from 0, to max_loop
-> moves[0], 1,2 and so on

Be prepared, that stuff is untested ^^
Hey, sorry for the late response. I ended up finding the fix so I thought I'd leave it here in case anyone else has the same issue as me.

for row, token in moves: 
        if row == 'a':  
         goto (-300,row_a_y_cord[0]) 
         del row_a_y_cord[0] 
        if token == 1: 
         dot(10, "red")
        elif token == 2:
         dot(10, "blue")
        elif token == 3:
         dot(10, "yellow")
        elif token == 4:
         dot(10, "green")

Turns out just using the list itself with the loop will loop it correctly