Download presentation
Presentation is loading. Please wait.
1
Intro to Robots Conditionals and Recursion
2
Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we throw away the remainder. To capture the remainder (before we lose it) we use the modulus (%) operator. then 7 / 3 = 2 7 % 3 = 1 n % d = r n / d = q n = q * d + r
3
Intro to Robots Use of Modulus Some numbers, like credit card or identification numbers, are encoded so that the last digit can be “calculated” from the previous digits. Suppose a 16-digit number has its first 15 digits as and that the 16 th digit is calculated by adding the digits as eight 2-digit numbers Repeat until there are only two digits the larger of the last two digits is the 16 th digit 4591-3295-6843-256_ 04 + 59 + 13 + 29 + 56 + 84 + 32 + 56 = 333 03 + 33 = 36 4591-3295-6843-2566
4
Intro to Robots Exercise Which if these two 16-digit numbers is “valid”? Or In a short while we’ll see an algorithm for verifying that a 16 digit number is valid using this method of calculating the last digit. 5291-4528-7426-6539 5291-4582-7426-6539
5
Intro to Robots Boolean Expressions: A boolean expression is one that is either true or false. In Python, true and false are represented as 1 and 0. The operator == compares two numbers and decides if they are equal Other such operators for numbers are >>> 5 == 6 0 >>> 5 == 5 1 x != y -- x is not equal to y x > y -- x is greater than y x = y -- x is greater than or equal to y x <= y -- x is less than or equal to y x == y -- x is equal to y
6
Intro to Robots BE CAREFUL!! The operator = is the “assignment operator” not the “comparison operator”. x = y -- the variable x takes on the value of the variable y x == y -- returns 1 of x and y have the same value; 0 otherwise
7
Intro to Robots Exercise What does the following do? We can use a function definition to capture this logic without any mistakes print ‘even’*(a%2==0) + ‘odd’*(a%2==1) def parity(a): return print ‘even’*(a%2==0) + ‘odd’*(a%2==1)
8
Intro to Robots Boolean or Logical Operators: There are three logical operators – and, or and not. Their meaning (semantics) are the same as in English. x > 2 and x 2 and x = 10 -- same as above
9
Intro to Robots Conditional Execution: if x > 0: print ‘x is positive’ condition; if true, everything indented is executed HEADER: STATEMENT 1 STATEMENT 2... STATEMENT n HEADER: starts a new line, ends in a : BODY: sometimes called a program block. Everything indented after the header.
10
Intro to Robots Alternative Execution: This can be encapsulated in a function to hide the code details: Or using the function we wrote earlier if x %2 == 0: print x, ‘is even’ else: print x, ‘is odd’ executed if condition is true executed if condition is false def printParity(x): if x %2 == 0: print x, ‘is even’ else: print x, ‘is odd’ print x, “is” + parity(x)
11
Intro to Robots Chained Conditionals: What happens if you need to choose from among three or more things (instead of just two): if x > 0: print x, ‘is positive’ elif (x == 0): print x, ‘is zero’ else: print x, ‘is negative’
12
Intro to Robots Exercise: Write a function called compare(x,y) that does the following: –Returns 1 if x > y –Returns 0 if x == y –Returns -1 if x < y def compare(x,y): if x > y: return 1 elif x == 0: return 0 else: return -1
13
Intro to Robots Exercise: Verify a 16-digit number.
14
Intro to Robots Nested Conditions: Decision Tree
15
Intro to Robots Nested Conditions if x == ‘A’ and y == 0: Do A0 elif x == ‘A’ and y == 1: Do A1 elif x == ‘B’ and y == 0: Do B0 else: Do B1 if x == ‘A’: if y == 0: Do A0 else: Do A1 else: if y == 0: Do B0 else: Do B1 if x == ‘A’ and y == 0: Do A0 elif x == ‘A’ and y == 1: Do A1 elif x == ‘B’ and y == 0: Do B0 elif x == ‘B’ and y == 1: Do B1 if x == ‘A’: if y == 0: Do A0 elif y == 1: Do A1 elif x == ‘B’: if y == 0: Do B0 elif y == 1: Do B1
16
Intro to Robots Return Statement: The classic reason for a function is to calculate a value (y) for a given value (x). In Python, the value returned by a function is expressed in a return statement. y = f(x) def min(a,b): # returns the smallest of two numbers if a < b: return a else: return b
17
Intro to Robots More than one return type: The built-in math function sqrt() fails on a negative number This version of square root “gently” avoids an error message if you try to calculate the square root of a negative number
18
Intro to Robots Recursion: def countDown(n): if n == 0: print “Blast Off!” else: print n countDown(n-1)
19
Intro to Robots Definition: A recursive definition is one that has two properties: –A trivial (base) case with a trivial definition –The definition describes something of a certain size (n) in terms of the same thing but of a smaller size (n-1). Problems can have non-recursive and recursive definitions/descriptions. Not all problems can be described recursively
20
Intro to Robots Towers of Hanoi ABC Move one disk from A to B Move one disk from A to C Move one disk from B to C Move one disk from A to B Move one disk from C to A Move one disk from C to B Move one disk from A to B
21
Intro to Robots Towers of Hanoi Problem: Problem: Move n disks from ‘A’ to ‘B’ Using ‘C’ Solution: Move the disks using the following rules: –You may only move one disk at a time –You may never put a big disk on top of a small disk Problem: Move n disks from ‘A’ to ‘B’ Using ‘C’ Solution: 1.Move n-1 disks from ‘A’ to ‘C’ using ‘B’ 2.Move 1 disk from ‘A’ to ‘B’ 3.Move n-1 disks from ‘C’ to ‘B’ using ‘A’ Non-recursive Definition Recursive Definition How! What! same problem; smaller size problem size base case
22
Intro to Robots Recursion and the Stack: Remember that each time a function is called space is reserved for its variables and parameters on “the Stack”. We saw this by introducing an error in the base case of the definition of the Towers of Hanoi program (divide by 0) and when the error occurred, the Python environment prints out the stack.
23
Intro to Robots n = (3-1) = 2 n = (2-1) = 1 n = (1-1) = 0 Error: division by 0 _toplevel_ hanoi n = 3 hanoi n = 2 hanoi n = 1 hanoi n = 0 also called main
24
Intro to Robots Infinite Recursion: What happens if there is no base case. def countDown(n): countDown(n-1) stack gets to be too big _toplevel_ countDown n = 3 countDown n = 2 countDown n = 1 countDown n = 0 countDown n = -1 countDown n = -2 countDown n = -3 countDown n = -4......
25
Intro to Robots Keyboard Input: Python has several built-in keyboard read functions. raw_input() reads keyboard input into a string until you hit return. raw_input() you can supply a prompt
26
Intro to Robots Keyboard Input: You can use the input() function if the input is integer type. Just make sure you enter an integer and not something else.
27
Intro to Robots Keyboard Input: The myro system also comes with its own keyboard input environment; a separate dialog box that is invoked by calling ask(“A Prompt”)
28
Intro to Robots Exercise:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.