How Do Robots Find Their Way? Conditionals and Repetition
http://en.wikipedia.org/wiki/file:cyclope_robot.jpg
http://www.youtube.com/watch?v=_l9rklaskwu
Learning Objectives Learn basic programming concepts (variables, if statements, loops, and functions) and write simple programs using these concepts.
Part 1 Robot behavior Conditionals Logic More complex conditionals Maze solving Part 2 Repeating behaviour While loops Loops in Maze Solving
Robots are dumb What does a robot need to know how to do to solve a maze? What commands and behaviors would be useful?
Can I move forward?
If these is empty space, then move forward
If these is empty space, then move forward Conditional
Part 1 Robot behavior Conditionals Logic More complex conditionals Maze solving Part 2 Repeating behaviour While loops Loops in Maze Solving
It is raining Wear a raincoat
Grade is at least 50 Pass the class
There is empty space in front of the robot The robot moves forward
True or false, aka Boolean value There is empty space in front of the robot The robot moves forward
Part 1 Robot behavior Conditionals Logic More complex conditionals Maze solving Part 2 Repeating behaviour While loops Loops in Maze Solving
and Everything has to be true
and true and true = true true and false and true = false false and false = false
or Only one thing has to be true
or true or true = true true or false or true = true false or false = false
What will the result of this expression be? (true and (not false) or (true and not false))
What will the result of this expression be? ((not false) or (false and (not true)) and false)
What will the result of this expression be? ((not false) or (false and (not true)) and false)
Using the logic at right, suppose our robot decides to pivot left. Which of the following can be true? (a) (b) (c) There is a wall only behind the robot and to its left. There is a wall only behind the robot. There is a wall to the left and right of the robot, but not in front. if (wall on left) and (no wall on right) and (no wall on front): then pivot right otherwise: pivot left Text 37607 90274: a 90277: b 90279: c
Part 1 Robot behavior Conditionals Logic More complex conditionals Maze solving Part 2 Repeating behaviour While loops Loops in Maze Solving
Anything that results in true or false can go into the if part of a conditional
I am buying a movie ticket and I am a student I will get a discount on the price
My percentage is at least 77 and my percentage is at most 79 My grade is B+
The battery is dead or there is no gas The car will not start
Not married and not engaged and like it Put a ring on it
Boolean Expressions in Python Type the following into the shell: 5 == 5
Boolean Expressions in Python Type the following into the shell: 5 == 5 Equality Operator
Boolean Expressions in Python Type the following into the shell: 5 == 4
Boolean Expressions in Python Type the following into the shell: 5!= 4
Boolean Expressions in Python Type the following into the shell: 5!= 4 Inequality Operator
If-Statements in Python Type the following into the shell: if 5 == 5: print(1)
If-Statements in Python Here is the syntax for if-statements in Python: if <a Boolean expression>: <code to run if the Boolean expression is true>
If-Statements in Python Here is the syntax for if-statements in Python: Something that must be either true or false if <a Boolean expression>: <code to run if the Boolean expression is true>
If-Statements in Python Here is the syntax for if-statements in Python: "Then" after this we must if <a Boolean expression>: indent <code to run if the Boolean expression is true>
If-Statements in Python Here is the syntax for if-statements in Python: if <a Boolean expression>: <code to run if the Boolean expression is true> Indentation tells Python how much code is part of the "then"
If-Statements in Python Here is the syntax for if-statements in Python: if <a Boolean expression>: <code to run if the Boolean expression is true> Everything with the same indentation will run when the Boolean expression is true
If-Statements in Python What will get printed here (if anything)? if 7 > 6 and (3 > 4 or 4 < 5): print(1)
If-Else Statements in Python Here is the syntax for if-else statements in Python: if <a Boolean expression>: <code to run if Boolean expression is true> else: <code to run if the Boolean expression is false>
If-Else Statements in Python Here is the syntax for if-else statements in Python: if <a Boolean expression>: <code to run if Boolean expression is true> else: "Otherwise" <code to run if the Boolean expression is false>
What is the output? x = 25 if x < 15: if x > 8: print(1) else: print(2) else: print(3) Text 37607 96596: 1 96920: 2 96921: 3 96925: 1 2 96927: 1 3 96928: 2 3
Part 1 Robot behavior Conditionals Logic More complex conditionals Maze solving Part 2 Repeating behaviour While loops Loops in Maze Solving
How would you find your way out of a maze? http://en.wikipedia.org/wiki/hedge_maze
Right-Hand Rule Same idea, but with left hand: https://www.youtube.com/watch?v=na137qgmz4s
One Move with Right-Hand Rule 1. If there is empty space to my right, then pivot right and step forward. 2. Otherwise, if there is empty space in front of me, then step forward. 3. Otherwise, if there is empty space to my left, then pivot left and step forward. 4. Otherwise pivot right without stepping forward.
There is a wall, so no
No wall in front, so yes!
There is a wall, so no
There is a wall, so no
No wall to the left, so yes!
Maze Solving in Python Remember, functions (aka routines) can "return" results!
Maze Solving in Python Suppose these functions existed in Python, and returned Boolean values: emptyspacetoright() emptyspacetoleft() emptyspaceinfront()
Maze Solving in Python Suppose these functions existed in Python, and simply caused the robot to take the associated action: pivotleft() pivotright() stepforward()
Maze Solving in Python Then one step in maze-solving would look like this: if emptyspacetoright(): pivotright() stepforward() elif emptyspaceinfront(): stepforward() elif emptyspacetoleft(): pivotleft() stepforward() else: pivotright()
Part 1 Robot behavior Conditionals Logic More complex conditionals Maze solving Part 2 Repeating behaviour While loops Loops in Maze Solving
How many times do I move before I m out of the maze??
Instead: I ll just keep moving until I get out
Loops Drive the same track multiple times: each lap is exactly the same
while loop Drive the track while the race is not over
while not at exit, continue making moves
Part 1 Robot behavior Conditionals Logic More complex conditionals Maze solving Part 2 Repeating behaviour While loops Loops in Maze Solving
While Loops in Python while <Boolean value>: <loop body>
While Loops in Python while <Boolean value>: <loop body> While loop: Drive around the track...
While Loops in Python while <Boolean value>: <loop body>...running this code again on every lap around the track...
While Loops in Python while <Boolean value>: <loop body>...until this Boolean is no longer true
While Loops in Python while <Boolean value>: <loop body> And yes, the colon the indentation are still important!
Example x = 0 while x < 2: x = x + 1 print(x)
Example x is given a value of 0 x = 0 while x < 2: x = x + 1 print(x)
Example The Boolean expression is checked x = 0 while x < 2: x = x + 1 print(x)
Example x = 0 while x < 2: x = x + 1 print(x) 1 is added to x, so x is now 1
Example x = 0 while x < 2: x = x + 1 print(x) 1 is printed to the shell
Example x = 0 while x < 2: x = x + 1 print(x) Lap one is now finished
Example x = 0 while x < 2: x = x + 1 print(x) so check the Boolean expression again
Example x = 0 while x < 2: x = x + 1 print(x) x becomes 2
Example x = 0 while x < 2: x = x + 1 print(x) 2 is printed to the shell
Example x = 0 while x < 2: x = x + 1 print(x) Lap two is now finished
Example x = 0 while x < 2: x = x + 1 print(x) so check the Boolean expression again Now that x is 2, the expression is false, and we're done!
What is the output? x = 6 while x > 4: print(x) x = x - 1 Text 37607 364495: 6 5 364508: 6 5 4 364513: 5 4 364514: 5 4 3 364517: 6 5 4 3
What is the output? n = 3 while n > 0: if n == 5: n = -99 print(n) n = n + 1 Text 37607 245374: 3 4 245390: 3 4 5 361200: 3 4-99 361203: 3 4 5-99
Part 1 Robot behavior Conditionals Logic More complex conditionals Maze solving Part 2 Repeating behaviour While loops Loops in Maze Solving
What does "driving a lap" mean in the context of robot maze solving?
Maze Solving Logic 1. while I have not reached the exit: 1. If there is empty space to my right, then pivot right and step forward. 2. Otherwise, if there is empty space in front of me, then step forward. 3. Otherwise, if there is empty space to my left, then pivot left and step forward. 4. Otherwise pivot right without stepping forward.
Maze Solving in Python Suppose that in addition to our previous functions, the following was also defined in Python: reachedexit()
Maze Solving in Python Then our whole maze-solving logic would look like this: while not reachedexit(): if emptyspacetoright(): pivotright() stepforward() elif emptyspaceinfront(): stepforward() elif emptyspacetoleft(): pivotleft() stepforward() else: pivotright()
Part 1 Robot behavior Conditionals Logic More complex conditionals Maze solving Part 2 Repeating behaviour While loops Loops in Maze Solving