Narmin
Narmin

Reputation: 11

How to execute 'for i in range' loop twice?

I am trying to make Karel pick three batches of beepers each consisting of ten beepers. I want 'move_batch()' to be called twice but it is executed 3 times because of the first 'for i in range(3):' command. When Karel moves 3 times, it meets the wall and crashes. How can I call 'move_batch()' 2 times using 'for i in range()' so that Karel does not crash to the wall link?

from karel.stanfordkarel import *

def main():
   move()

   for i in range(3):
      pick_ten_beepers()
      move_batch()

def pick_ten_beepers():
   for i in range(10):
      pick_beeper()

def move_batch():
   move()
   move()

Upvotes: 0

Views: 295

Answers (1)

Ravi Kulkarni
Ravi Kulkarni

Reputation: 667

you can add if condition in for loop where i should be less than equal to 1.

def main():
   move()

   for i in range(3):
      pick_ten_beepers()
      if i <= 1:
        move_batch()

def pick_ten_beepers():
   for i in range(10):
      pick_beeper()

def move_batch():
   move()
   move()

Upvotes: 1

Related Questions