-2

I want to make my script as short as possible:

from turtle import *
for _ in range(10):
    lt(72)
    fd(71)
    rt(108)
    fd(71)
for _ in range(10):
    for s in [(29,90),(73,72),(73,90),(29,72)]:
        fd(s[0])
        rt(s[1])

As you can see, there are two for-loops are "for _ in range 10:".

Is there a way I could merge the two loops, and still get the same result?

Ann Zen
  • 17,892
  • 6
  • 20
  • 39
  • 2
    I don't think so, because everything in the second loop needs to be done after all the actions in the first loop, you can't mix them together. – Barmar May 19 '20 at 17:07
  • @Barmar Yes, I was wondering if we can delay something in a for-loop until a certain iteration. – Ann Zen May 19 '20 at 17:09
  • 1
    You can do it like in the answer, but it's silly. If the two loops do different things, they should be separate. – Barmar May 19 '20 at 17:15

3 Answers3

3

You can put everything in a list, which will consume a bit more space:

for walk, turn in [(0,-72),(71,108),(71,0)]*10+[(29,90),(73,72),(73,90),(29,72)]*10:
    fd(walk)
    rt(turn)
trincot
  • 211,288
  • 25
  • 175
  • 211
0

If you only want one loop, try the following:

for i in range(20):
    if i < 10: 
       lt(72)
       fd(71)
       rt(108)
       fd(71)
    if i > 10: 
        for s in [(29,90),(73,72),(73,90),(29,72)]:
           fd(s[0])
           rt(s[1])
ananyamous
  • 151
  • 1
  • 8
-1

This looks a little unnecessary, but if you really really want to consolidate the two loops, you can try:

from turtle import *

mode1 = True
for i in range(20):
    if mode1:
        lt(72)
        fd(71)
        rt(108)
        fd(71)
        if i == 9:
            mode1 = False
    else: 
        for s in [(29,90),(73,72),(73,90),(29,72)]:
            fd(s[0])
            rt(s[1])
Ashwin
  • 46
  • 5