Control Structures - while/if
Today: loops, if-statement, maybe variables
See links to Python Guide front page: loops, if-logic, booleans
Big Picture: Patience + Legos
- Early on, learning many little pieces
bit.move()
while
if
- Each piece is simple and mechanical
- Be patient
- Each lecture adds 5 more pieces
- With simple pieces .. put them together to make something great
- CS nutshell: each line is trivial, but programmer can make a masterpiece with them
- Next week we'll be there, but this week's output is limited
Hey Up Front: bit.front_clear()
bit.move() function modifies bit+world
- Other functions look at bit+world, no modify
- Test if way-forward is clear before a move:
bit.front_clear()
- Calling
bit.front_clear() returns a boolean value True or False
-True and False are Python's "boolean" values
-Boolean values fit into "while" below
-True here means the square available, move() is ok
-False here means the square is blocked, move() would be an error
-Regular squares are clear (even if painted)
-Black blocks and the world-edge are not clear
While Loop - Power!
- Loop - huge jump up in power
- "Do these lines again and again"
- Every algorithm has key lines to run thousands or millions of times
- Loops are the way to do that
- Several types of loop - today while
While Loop Demo
>
Loop Demo
- This code is complete
- Read problem statement
- Run Case-1, play with it to see how it works
- Like Jurassic Park - while loop frozen in amber!
- Slide the Steps back to start, use Step button to go one at a time
- See the order of test / body-lines
-test, body-lines, test, body-lines, ...
- Question: Suppose world is width 1. What is the result?
-Case 3, turn off auto-play. Predict what this will do, then step through it
For reference, here is the code:
def loop_demo(filename):
bit = Bit(filename)
bit.paint('green')
while bit.front_clear():
bit.move()
bit.paint('blue')
bit.paint('red')
While Loop Syntax
Syntax 4 parts: while, test, colon, indented body lines
while boolean-test:
body lines inside the while
indented
While Loop Operation
- First check the test at the top, True or False?
- If True, run all the lines of the body, top to bottom
-then loop back to top, check test again, .. repeat
- When the loop-test is False, loop is done, running continue after the loop body
- If the test is False initially, the body runs zero times (see Case-3)
While Loop Observations
- Lines before and after loop run once, as usual
- Body lines in the loop are different, running many times
- The test is checked, then run all the body lines top to bottom
- It's possible for the body to run zero times
- Big picture: code generality
-There is one copy of your code algorithm
-"General" - it should work for all the different input worlds
-Try Run All option
If Statement - Logic
While loop is power. If-statement is control, controlling if lines are run or not
Problem statement: for each moved-to square, if it is blue, change it to green. Run it, then we'll talk about how the if-statement works.
>
If Demo
For reference here is the code:
def if_demo(filename):
bit = Bit(filename)
while bit.front_clear():
bit.move()
if bit.get_color() == 'blue':
bit.paint('green')
If-Statement Deconstruction
Here is the key line of code:
....
if bit.get_color() == 'blue':
bit.paint('green')
....
1. bit.get_color() - Returns
- Calling
bit.get_color() function, returns the color value of the current square
'red' 'green' 'blue', or None if square is empty
- Look at code above
- Like
bit.get_color() is replaced by 'blue' or 'red' or whatever, then the line is evaluated
2. == Compares Two Values - Boolean
- The operator
== compares two values for equality
-(two equal signs next to each other)
- Returns True if equal, False otherwise
- e.g.
'blue' == 'blue' is True
- Comparison does not use single equal sign (
=)
- Easy to type
= by accident, Python gives an error fortunately
- Not Equal Test:
!=
e.g. if num != 6:
3. How If Demo Works
- Look at the if-demo code
- For each moved-to square
- run an if-test on color of that square
- If the test is True, paints it green
If Statement Syntax
Syntax 4 parts (similar to "while"): if, boolean-test, colon, indented body lines
if boolean-test:
body lines inside the if
indented
If Statement Operation
- If statement evaluates the test code, looking for True or False
- If test is True, runs the body top to bottom
- Otherwise skips the body, run continues after the body
- Basically an On/Off switch - runs the body lines or runs nothing
With the basic if-structure in place, here are more boolean features, then we'll put it all together.
Bit Test: bit.left_clear()
bit.left_clear()
- Returns True if square to left is clear, false otherwise
-So if True, could turn left and move
- Also have
bit.right_clear()
Boolean Expressions: and or not
- Something like
bit.left_clear() returns a "boolean" True/False
- These operators combine boolean values:
and or not
- xx and yy = both sides must be True, then it is True
- xx or yy = either side or both sides are True, it is True
- not xx = inverts True/False
Boolean Examples..
Q1: when is the following if-test True?
if bit.left_clear() and bit.right_clear():
# when does this happen?
Q2: when is the following if-test True? (just changing and to or)?
if bit.left_clear() or bit.right_clear():
# when does this happen?
Q3: when is the following if-test True?
if not bit.left_clear():
# when does this happen?
Answers..
A1: When either both left and right are clear
A2: When either left or right are clear (or both)
A3: When the left is blocked (i.e. lear_clear() returns False)
Put It All Together - Applied Boolean Code
>
Bit Logic1 Exercises
First 3 are demos - walk through them
Then last 3 are little exercises to try .. see how many we get through today