Handouts: Karel Reference
File: beeper_line.py

Your goal is to fill the first row of Karel's world with beepers. You can assume that Karel starts in the bottom left corner, facing east. For example, if you ran your program on the world on the left, it should produce the world on the right.


Your program should also work for worlds of different sizes. For example if we run the EXACT same program on this slightly larger world, Karel should still be able to place a full line of beepers without crashing. This means we can't use a for loop. We don't know beforehand how big the world is going to be!


Solution

from karel.stanfordkarel import *

"""
File: beeper_line.py
------------------------------
Places a row of beepers on the bottom row of Karel's world.
Works with any size world.
"""


def main():
    while front_is_clear():
        put_beeper()
        move()

    """
    the line below is necessary to place the final beeper.
    the number of times Karel moves is one less than the number
    of times Karel places a beeper (if the world is five squares
    wide, we place 5 beepers, but only move 4 times).
    """
    put_beeper()


# There is no need to edit code beyond this point

if __name__ == "__main__":
    run_karel_program()