Python Forum
How would I go about repeating/looping this code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How would I go about repeating/looping this code?
#1
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?
Reply
#2
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 ?
Reply
#3
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.
Reply
#4
Why don't you try things out and see if you can get it to work?
Reply
#5
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 ^^
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  repeating pastakipp 1 1,561 Oct-24-2020, 03:28 PM
Last Post: jefsummers
  Python code isn't looping player turns Reta 7 3,697 May-19-2019, 02:27 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020