How Do Robots Find Their Way?

Similar documents
Flow Control: Branches and loops

Text Input and Conditionals

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Conditionals and Recursion. Python Part 4

Pupil Name. Year. Teacher. Target Level. Key Stage 3 Self-Assessment Year 9 Python. Spelling Test No 3. Spelling Test No 2. Spelling Test No 1

3. Conditional Execution

Unit E Step-by-Step: Programming with Python

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

SELECTION. (Chapter 2)

Chapter 3. Iteration

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Intro. Scheme Basics. scm> 5 5. scm>

Boolean Expressions and if 9/14/2007

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Flow Control. So Far: Writing simple statements that get executed one after another.

Repetition Algorithms

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

CONTROL AND ENVIRONMENTS 1

3. Conditional Execution

Comp 151. Control structures.

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

Making Decisions In Python

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Lecture Transcript While and Do While Statements in C++

Programming Fundamentals

CONTROL AND HIGHER ORDER FUNCTIONS 1

KAREL JR 3 STUDENT JOURNAL REVISED APRIL 19, 2017 NAME SCHOOL, CLASS, PERIOD

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

Decisions, Decisions. Testing, testing C H A P T E R 7

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Control Structures 1 / 17

Quiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist.

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Lecture 02 Making Decisions: Conditional Execution

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

CSE 115. Introduction to Computer Science I

The L Line. The Express Line to Learning. Wiley Publishing All Rights Reserved.

Comp 151. Control structures.

ENGR 101 Engineering Design Workshop

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

CS 111X - Fall Test 1

LN #2 (3 Hrs) Variables, Sequence Boolean Logic & Selection CTPS Department of CSE,Coimbatore

MITOCW watch?v=0jljzrnhwoi

CSE 113 A. Announcements - Lab

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

RECURSION AND LINKED LISTS 3

Introduction to Python

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Structure and Interpretation of Computer Programs Spring 2016 Test 1

Python Activity 7: Looping Structures WHILE Loops

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Exercise 6 - Addressing a Message

Python for Non-programmers

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

CS1 Lecture 3 Jan. 18, 2019

roboturtle Documentation

C ONTROL AND H IGHER O RDER F UNCTIONS

Honors Computer Science Python Mr. Clausen Program 7A, 7B

CS Summer 2013

Programming Logic and Design Sixth Edition

CS1 Lecture 3 Jan. 22, 2018

Line tracking sensors and algorithms

CS 1301 Exam 1 Answers Fall 2009

cs1114 REVIEW of details test closed laptop period

Unit 7. 'while' Loops

Python Activity 5: Boolean Expressions and Selection Statements

C/C++ Programming Lecture 7 Name:

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

CS 105 Lab As a review of what we did last week a. What are two ways in which the Python shell is useful to us?

3 Programming. Jalal Kawash Mandatory: Chapter 5 Section 5.5 Jalal s resources: How to movies and example programs available at:

Using Functions in Alice

Built-in functions. You ve used several functions already. >>> len("atggtca") 7 >>> abs(-6) 6 >>> float("3.1415") >>>

Introduction to the Java Basics: Control Flow Statements

SCHEME AND CALCULATOR 5b

Python for Informatics

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Python Unit

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION

WELCOME UVAHS REMEDY INCIDENT MANAGEMENT

Data 8 Final Review #1

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Programming in Python 3

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

KAREL UNIT 5 STUDENT JOURNAL REVISED APRIL 19, 2017 NAME SCHOOL, CLASS, PERIOD

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Control of Flow. There are several Python expressions that control the flow of a program. All of them make use of Boolean conditional tests.

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

CS112 Lecture: Repetition Statements

Fundamentals of Programming (Python) Control Structures. Sina Sajadmanesh Sharif University of Technology Fall 2017

Transcription:

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