Error
Errors:
It’s the moment you’ve been working towards - the Ultimate Clean Up Challenge! Karel has put together a community litter pick up event for her neighborhood. For the next three weeks, Karel will work with other community members to pick up the litter in the park.
Write a program that has Karel walk around the park and pick up all of the litter. Your program should work regardless of which week it is.
Hint: Use what you’ve learned about if statements and for loops!
Challenge: Can you avoid repeating the same for loops by using a for loop within a for loop? To do this, you may need to use the following hints:
Method A: How can you get Karel to face the same direction at the start of each loop?
Method B: You can use an if/else statement to give Karel options when she asks a question about the world around her: one set of commands for when she answers yes and another set of commands for when she answers no. Check out the DOCS tab to learn how to write an if/else statement in Python. If you go this route, you might need Karel to ask questions about which direction she is facing by using
facing_east()
andfacing_west()
.
Ending World
Karel's Built in Commands
move() |
turn_left() |
|
put_ball() |
take_ball() |
UltraKarel commands
paint(color) |
UltraKarel conditions
random_chance() |
color_is(color) |
# Paints a square red. paint(color['red']) # Colors: color['red'] color['blue'] color['green'] color['yellow'] color['cyan'] color['orange'] color['white'] color['black'] color['gray'] color['purple'] # Determine if a square is a certain color if color_is(color['red']): move() Random # Returns true 50% of the time random_chance() # Returns true probability percentage of the time random_chance(probability) # Returns true 20% of the time. random_chance(0.2) # Example if random_chance(0.8): put_ball()
Functions
Writing a function is like teaching karel a new word.
Naming Functions: You can name your functions whatever you want, but you can't have spaces in the function name.
Remember that commands in functions must be indented one level
def turn_right(): turn_left() turn_left() turn_left() def turn_around(): turn_left() turn_left() def your_function_name(): # Code that will run when you make a call to # this function.
Calling a Function
You call a function to tell the computer to actually carry out the new command.
# Call the turnn_around() function once turn_around() # Call the turn_right() function 2 times turn_right() turn_right()
Conditional Statements
Remember that comands in conditional statements must be indented one level.if condition:
#code that will run if the condition is true
if condition:
#code that will run if the condition is true
else:
#code that will run if condition is not true
Example of if statements
if front_is_clear(): move() if balls_present(): take_ball() else: move()
Karel Conditions
Don't forget the()
at the end!
front_is_clear() left_is_clear() right_is_clear() facing_north() facing_south() facing_east() facing_west() balls_present() |
front_is_blocked() left_is_blocked() right_is_blocked() not_facing_north() not_facing_south() not_facing_east() not_facing_west() no_balls_present() |
Loops
Remember that commands in a loop statement must be indented one level.While Loops
while CONDITION: # Code that will run while the CONDITION is true. # Once the CONDITION is no longer true, # it will stop.
Example of while loops
# This moves Karel to a wall while front_is_clear(): move()
For Loops
for i in range(COUNT): # Code that will run 'COUNT' times
Example of for loops
# This puts down 10 balls */ for i in range(10): put_ball()You can have multiple statements or function calls in a for loop.
# This puts down five balls and moves after each one for i in range(5): put_ball() move()
Comments
""" A multi-line comment describes your code to someone who is reading it. """ # Use single line comments to clarify code.