-3

I decided to try to do a turtle project to learn the random commands. My question is in this code:

pos = 360
while a.ycor()<pos:
 a.forward(randint(1,5))
while b.ycor()<pos:
 b.forward(randint(1,5))
while c.ycor()<pos:
 c.forward(randint(1,5))
while d.ycor()<pos:
 d.forward(randint(1,5))
while e.ycor()<pos:
 e.forward(randint(1,5))

how can I get all the conditionals to execute at the same time to start the race (all turtles take off at the same time)?

martineau
  • 119,623
  • 25
  • 170
  • 301

3 Answers3

1

I'm not 100% sure what you're trying to do, so assuming you want to have multiple functions running at the same time, use either the multiprocessing or threading modules. Quick multiprocessing example:

import multiprocessing, time

def aFunction(aVar):
    time.sleep(5)
    print(f"Ye {aVar}")

if __name__ == '__main__':
    multiprocessing.freeze_support()

    pool = multiprocessing.Pool(4)
    pool.map(aFunction, [X for X in range(4)])
    input("Press Return to exit")
FierySwordswoman
  • 181
  • 1
  • 2
  • 7
0

You may try to use the logical operator AND:

pos = 360
while a.ycor()<pos and
      b.ycor()<pos and
      c.ycor()<pos and
      d.ycor()<pos and
      e.ycor()<pos:

     a.forward(randint(1,5))
     b.forward(randint(1,5))
     c.forward(randint(1,5))
     d.forward(randint(1,5))
     e.forward(randint(1,5))
freude
  • 3,632
  • 3
  • 32
  • 51
0

To avoid repetition, you could have your variables and methods in a dictionary and then loop through them.

pos = 360

d = {a: a.ycor, b: b.ycor, c: c.ycor, d: d.ycor, e: e.ycor}

for k, v in d.items():
    while v() < pos:
        k.forward(randint(1, 5))
srikavineehari
  • 2,502
  • 1
  • 11
  • 21