Lecture 3
Lecture 3
Lecture 3
Instruction set
Arithmetic and logical operations
+, -, *, /, and ** for defining
and, or, not expressions
Assignment
Conditionals
Iterations
Input/output
No pointers
No declarations
OUTLINE
Conditionals
Iterations
for_loops
while_loops
CONDITIONALS
A condition is something that is either True or False.
if <condition>
True block :
False block
else:
if not hubo.on_beeper():
hubo.drop_beeper()
def move_or_turn():
if hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
def dance():
for i in range(4): #Dance by turning four times to the left
hubo.turn_left()
def move_or_turn():
if hubo.front_is_clear():
dance()
hubo.move()
else:
hubo.turn_left()
hubo.drop_beeper()
For i in range(18):
move_or_turn() ## Ex: 1
dancing.py
MULTIPLE CHOICES
if hubo.on_beeper():
hubo.pick_beeper()
else:
elif hubo.front_is_clear():
if hubo.front_is_clear():
hubo.move()
hubo.move()
elif hubo.left_is_clear(): else:
hubo.turn_left() if hubo.left_is_clear():
elif hubo.right_is_clear(): hubo.move()
turn_right() ---------------------------
else:
turn_around()
MULTIPLE CHOICES
hubo.drop_beeper()
hubo.move()
while not hubo.on_beeper():
if hubo.right_is_clear():
turn_right()
hubo.move()
elif
hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
Does this always
work?
How about this
case?
hubo.drop_beeper()
hubo.move()
while not hubo.on_beeper():
if hubo.right_is_clear():
turn_right()
hubo.move()
elif
hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
Does this work? hubo.drop_beeper()
while not
hubo.front_is_clear():
hubo.turn_left()
hubo.move()
while not hubo.on_beeper():
if hubo.right_is_clear():
turn_right()
hubo.move()
elif
hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
Does this always work?
Hubo.turn_left()
Still not perfect !
Very sensitive to the initial position of
Hubo.
hubo.drop_beeper()
while not
hubo.front_is_clear(): def
hubo.turn_left() mark_and_move():
hubo.move()
while not
hubo.on_beeper(): def follow_wall():
if hubo.right_is_clear():
turn_right() mark_and_move()
hubo.move()
while not
elif
hubo.on_beeper():
hubo.front_is_clear():
hubo.move() follow_wall()
else: hubo.turn_left()
hubo.turn_left()
Hubo.turn_left()
“””
This program lets the robot go around his world counter-
clockwise, stopping when he comes back to his starting point.
“””
#Turn right.
def turn_right():
for i in range(3):
hubo.turn_left()
#Mark the starting point and move
def mark_and_move():
hubo.drop_beeper()
while not hubo.front_is_clear():
hubo.turn_left()
hubo.move()
(continued)
#Follow the wall at each iteration.
def follow_wall():
if hubo.right_is_clear():
# turn right to follow the wall
right_turn()
hubo.move()
elif hubo.front_is_clear():
# move forward while following the wall
hubo.move()
else:
# turn left to follow the wall
hubo.turn_left()
(continued)
#Begin actual move.
mark_and_move()
#Follow the entire wall.
while not hubo.on_beeper():
follow_wall()
Hubo.turn_left()
STEPWISE REFINEMENT
1. Start with a primitive program that solves a simple
problem.
2. Make small changes, one at a time to generalize the
program.
3. Make sure that each change does not invalidate
what you have done before.
4. Add appropriate comments (not just repeating what
the instruction does).
5. Choose descriptive names.