Handouts: KarelReference
Worked Examples: Invert, UN Karel
Day1: random_painter_karel.py


Karel is starting to find its world too black and white. Help it fill it with color.

Paint and Random

Karel has a few commands up its sleeve. It can paint the corner on which it is standing using the instruction


The value enclosed in the parentheses is called an argument in the terminology of programming languages, and it may be any one of RED, ORANGE, GREEN, CYAN, BLUE, MAGENTA, and so on. So you can call paint_corner(RED) to paint the square that Karel is standing on into a red square.

We'll talk about randomness later, but for now know that we want to draw one of two colors, and we need to do so randomly in Karel. The new condition random(0.5) is a condition that is True half of the time, but in an unpredictable (random) way. Like flipping a coin, sometimes this condition is True and sometimes it is False. For example, the code

if random(0.5):

will place a beeper (or will not) with equal probability. If you wanted, say, on average 75% of the time to have a beeper (instead of 50%), you could change the condition to random(0.75).


Use these new abilities to have Karel randomly paint each corner of a world one of two colors. You can chose any two colors you would like; we chose green and blue.

Below are three executions of the same program that randomly paints any sized world green and blue executed on a 10x10 world. Note that because of the random condition, the pattern of green and blue is different on each run!

As always, remember to decompose your solution! Your program should work on a world of any size.