Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line...

91
Control Flow Chris Gregg CS106A, Stanford University Modified from slides courtesy of Chris Piech and Mehran Sahami

Transcript of Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line...

Page 1: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Control FlowChris Gregg

CS106A, Stanford UniversityModified from slides courtesy ofChris Piech and Mehran Sahami

Page 2: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Install PyCharm

Please follow instructions closely.Email Brahm if you have problems.

Page 3: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Using Karel and Assignment 1

• Reading: Should read the “Karel Reader” on class website• Handout #3: “Honor Code”• Handout #4: “Using Karel with PyCharm”

– Tells you how to get started with writing Karel programs• Handout #5: “Assignment 1”

– Set of Karel programs for you to write– Due 10:30am (PDT) on Tuesday, June 30th

• Only use features of Karel in the course reader– No other features of Python may be used in Karel programs!

Page 4: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

+ + + + +

+ + + + +

+ + + + +

1

2

3

1 2 3 4 5

North

South

West East

Recall, Karel’s World

“Streets” run East/West

“Avenues” run N

orth/South

• Grid, where “corner” is intersection of each street/avenue• Karel is currently on corner (1, 1)• If Karel moved forward, Karel would be on corner (2, 1)• Karel’s beeper bag can have 0, 1, or more (up to infinite) beepers

Page 5: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

from karel.stanfordkarel import *

"""File: StepUpKarel.py--------------------Karel program, where Karel picks up a beeper,jumps up on a step and drops the beeper off."""

def main():move()pick_beeper()move()turn_left()move()turn_right()move()put_beeper()move()

# Karel turns to the rightdef turn_right():

turn_left()turn_left()turn_left()

First Lesson in Programming Style

Multi-line comment

One line comment

SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable by humans

Descriptive names

(snake_case)

Page 6: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Today’s Goal

1. Code using loops and conditions2. Trace programs that use loops and conditions

Page 7: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Today’s Route

for Loops

while Loops

if

if/else

ControlFlow

The Riv

er of Co

ntrol Flo

w

You are here

Page 8: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

for i in range(count):statements # note indenting

def turn_right():for i in range(3):

turn_left() # note indenting

for loop

Page 9: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

def main():for i in range(4):

put_beeper()move()turn_left()

Page 10: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

def main():for i in range(4):

put_beeper()move()turn_left()

Page 11: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

def main():for i in range(4):

put_beeper()move()turn_left()

Page 12: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

First time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 13: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

First time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 14: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

First time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 15: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

First time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 16: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Second time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 17: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Second time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 18: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Second time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 19: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Second time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 20: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Third time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 21: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Third time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 22: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Third time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 23: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Third time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 24: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Fourth time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 25: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Fourth time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 26: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Fourth time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 27: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Fourth time

through the

loop

def main():for i in range(4):

put_beeper()move()turn_left()

Page 28: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Square

Done!

def main():for i in range(4):

put_beeper()move()turn_left()

You need the postcondition of a loop to match the precondition

Page 29: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Today’s Route

for Loops

while Loops

if

if/else

ControlFlow

The Riv

er of Co

ntrol Flo

w

Page 30: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

while condition:statements # note indenting

def move_to_wall():while front_is_clear():

move() # note indenting

while loop

Page 31: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Conditions Karel Can Check ForTest Opposite What it checks

front_is_clear() front_is_blocked() Is there a wall in front of Karel?

left_is_clear() left_is_blocked() Is there a wall to Karel’s left?

right_is_clear() right_is_blocked() Is there a wall to Karel’s right?

beepers_present() no_beepers_present() Are there beepers on this corner?

beepers_in_bag() no_beepers_in_bag() Any there beepers in Karel’s bag?

facing_north() not_facing_north() Is Karel facing north?

facing_east() not_facing_east() Is Karel facing east?

facing_south() not_facing_south() Is Karel facing south?

facing_west() not_facing_west() Is Karel facing west?

This is in Chapter 10 of the Karel course reader

Page 32: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Task: Place Beeper Line

Page 33: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 34: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 35: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 36: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 37: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 38: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 39: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 40: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 41: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 42: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 43: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 44: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Page 45: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

Done!

BUGGY!

Page 46: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

put_beeper() # add final put_beeper

Fixed!Not in while loop

Page 47: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Place Beeper Line

def main():while front_is_clear():

put_beeper()move()

put_beeper() # add final put_beeper

Fixed!

Page 48: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Fence Post Problem

Also sometimes called an “Off By One Error”

Page 49: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

A program executes one line at a time.

The while loop checks its condition only at the start of the code block and before repeating.

Page 50: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Repeat Process

Which Loop

for Loop while Loop

Know how many times

(definite loop)

Don’t know how many times

(indefinite loop)

Page 51: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Actual Bug from Mark II ComputerActual Bug from Marc II

Page 52: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Grace Hopper

Page 53: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Today’s Route

for Loops

while Loops

if

if/else

ControlFlow

The Riv

er of Co

ntrol Flo

w

Page 54: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

if condition:statements # note indenting

def safe_pick_up():if beepers_present():

pick_beeper() # note indenting

if statement

Page 55: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Today’s Route

for Loops

while Loops

if

if/else

ControlFlow

The Riv

er of Co

ntrol Flo

w

Page 56: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

if condition:statements # note indenting

else:statements # note indenting

def invert_beepers():if beepers_present():

pick_beeper() # note indentingelse:

put_beeper() # note indenting

if-else statement

Page 57: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

You just learned most of programming “control flow”

Page 58: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Today’s Goal

1. Code using loops and conditions2. Trace programs that use loops and conditions

Page 59: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Putting it all togetherSteepChaseKarel.py

Page 60: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Steeple Chase

Page 61: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steeple

Page 62: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()

Page 63: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()

Page 64: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()

Page 65: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()

Page 66: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()

Page 67: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()

Page 68: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()

Page 69: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()

Page 70: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()

Page 71: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()

Page 72: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()

Page 73: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()

Page 74: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()

Page 75: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()

Page 76: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()

Page 77: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()

def move_to_wall():while front_is_clear():

move()

Page 78: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()

def move_to_wall():while front_is_clear():

move()

Page 79: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()

def move_to_wall():while front_is_clear():

move()

Page 80: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()

def move_to_wall():while front_is_clear():

move()

Page 81: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()

def move_to_wall():while front_is_clear():

move()

Page 82: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()

def move_to_wall():while front_is_clear():

move()

Page 83: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()turn_left()

def move_to_wall():while front_is_clear():

move()

Page 84: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()turn_left()

def move_to_wall():while front_is_clear():

move()

Page 85: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()turn_left()

def move_to_wall():while front_is_clear():

move()

You need the postcondition of a loop to match the precondition

Page 86: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()turn_left()

ascend_hurdle()

descend_hurdle()

Page 87: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepleturn_left()while right_is_blocked():

move()turn_right()move()turn_right()move_to_wall()turn_left()

ascend_hurdle()

descend_hurdle()

Page 88: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steeple

move()turn_right()move_to_wall()turn_left()

descend_hurdle()

def ascend_hurdle():turn_left()while right_is_blocked():

move()turn_right()

ascend_hurdle()

Page 89: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steeple

move()ascend_hurdle()

descend_hurdle()

def ascend_hurdle():turn_left()while right_is_blocked():

move()turn_right()

def descend_hurdle():turn_right()move_to_wall()turn_left()

Page 90: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

Focus on One Steepledef ascend_hurdle():

turn_left()while right_is_blocked():

move()turn_right()

def descend_hurdle():turn_right()move_to_wall()turn_left()

def jump_hurdle():ascend_hurdle()move()descend_hurdle()

Page 91: Control Flow...turn_left() turn_left() turn_left() First Lesson in Programming Style Multi-line comment One line comment SOFTWARE ENGINEERING PRINCIPLE: Aim to make programs readable

A Whole Program:SteepChaseKarel.py