250 likes | 410 Views
Randomness and Repetition. Dr. Paige H. Meeker. Notes from various text books, IPRE slides. Randomness…. from random import * Two functions: random() Returns a random number between 0.0 and 1.0 randrange (A,B) Returns a random number between A and B-1
E N D
Randomness and Repetition Dr. Paige H. Meeker Notes from various text books, IPRE slides
Randomness… from random import * • Two functions: • random() • Returns a random number between 0.0 and 1.0 • randrange(A,B) • Returns a random number between A and B-1 • Being able to do things randomly is an important concept in computer science. Here, we can make random movements with our robot, draw random numbers from a virtual hat, or any other number of interesting things!
User Input • User input can be used in a variety of ways. • We have discussed the commands “input” and “raw_input” – we can also take input from the user graphically.
Questions • askQuestion(“question here!!”) • Pops up a dialog box with the question and two boxes labeled “Yes” and “No” • askQuestion(“question here!!”,[“option1”, “option2”, …]) • Pops up a dialog box with the question and boxes for each option • You can now use this to get information from the user in a graphical way.
Making a List, and Checking it Twice • Ooops – wrong month. It’s not yet time for Santa. But, let’s talk about lists anyway. • Lists in programming languages are useful tools to gather a group of related information together for storage. • In Python, a list can be anything – numbers, letters, strings, Images, etc. • We have already seen a list today – the second example of the “askQuestion” command contains a list – let’s see how to manipulate a list.
Defining Lists • L = [] #this defines an empty list • Sevens = [7,14,21,28] • Fives = [5,10,15,20,25] • Names = [“Tom”, “Melody”, “Piper”, “Amber”, “Tango”] • Cities = [“Clinton”, “New York”]
Operations on Lists • len(L) #this will return the length of list “L” • Sevens + Fives #this will return the combined list: [7,14,21,28,5,10,15,20,25] • Names[0] = Tom • Names[3:5] = [Piper, Tango] • Names.sort() = [Amber, Melody, Piper, Tango, Tom] • Names.reverse() = [Tom, Tango, Piper, Melody, Amber] • Names.append(“Cherry”) = [Tom, Tango, Piper, Melody, Amber, Cherry] • 15 in Fives
Lists as Sequences • Lists can be used in for loops to perform repetitions: Classes = [“INTD 110”, “CSC 201”, “ENG 110”] for classes in Classes: print classes • Strings are sequences: ABC = “ABCDEFGHIJKLMNOPQRSTUVWXYZ” for letter in ABC: speak(letter)
Lists as Sequences • You can also convert sentences into lists by “splitting” up the words sentence = “Can you play blackjack” words=sentence.split() • words now contains a list: [“Can”, “you”, “play”, “blackjack”]
Repetition Humans get bored quickly when they have to repeat the same task over and over again. However, computers use this powerful concept to get a lot done. There are different ways computers use to repeat their steps: • Iteration • Loops • Recursion
While… • While statements are a looping construct that executes while some condition (or “Boolean expression”) is true. while BOOLEAN EXPRESSION : statement statement …. statement statement
While… • All the statements within the code block are executed if the BOOLEANEXPRESSION is true; none are executed if it is false. while BOOLEAN EXPRESSION : statement statement …. statement statement
What does this do? i = 1 while i < 4: beep(i,440) i = i + 1
What does this do? i = 1 while i > 4: beep(i,440) i = i + 1
What does this do? i = 1 while i < 4: beep(i,440) i = i - 1
Infinite Loops • When creating loops, you must be careful to set the Boolean expression in such a way that the condition is changed within the loop. If you do not, the loop will never end. while True: forward(1) #never gets to any other statement.
Nested Loops • Just like you can place “if” statements inside of one another, you can place loops inside of one another. You must remember their indention level j = 0 while j < 5: print(“outer loop”,j) k = 0 while k < 5: print (“inner loop”,k) k = k + 1 j = j + 1 print(“out of the loop!”)
Example :How many times does each loop print? j = 0 while j < 5: print(“outer loop”,j) k = 0 while k < 5: print (“inner loop”,k) k = k + 1 j = j + 1 print(“out of the loop!”)
Just “break” free! • If you need to escape the loop before the Boolean expression changes for some reason, the keyword “break” is the key to freedom: while True: forward(1) if (getStall()): break stop()
For Loops • While loops repeat actions in the code until some condition changes; for loops repeat code for some set amount of time. • for iterates through a sequence • Sequences can be lists or a range of objects • for i in range(5): • #do something 5 times • for i in [l1,l2,l3,l4,l5]: • #do something 5 times and assign i to the values in the list sequentially
Obstacle Avoidance using Robot’s Sensors #Avoiding Obstacles cruiseSpeed = 0.6 turnSpeed = 0.5 def main(): while timeRemaining(60): L, R = getIR() L = 1 - L R = 1 - R if getStall(): forward(cruiseSpeed,1) if L and R : move(0,turnSpeed) elif (L): move(-cruiseSpeed, -turnSpeed) elif R: move(-cruiseSpeed, turnSpeed) else: move(-cruiseSpeed,0) stop() main()
Wander - Avoiding Obstacles # uses IR sensors to wander while # avoiding obstacles def wander(seconds): start = currentTime() while currentTime() - start < seconds: forward(1) stop() • Timed behavior • What about avoiding the obstalces?
Wander - Avoiding Obstacles • Use the IR sensor on the robot to avoid obstacles • What about left and right IR sensors? # uses IR sensors to wander while # avoiding obstacles def wander(seconds): start = currentTime() while (currentTime() - start < seconds): c = getObstacle(“center”) if c > 0: beep(.2, 880) backward(1, .1) turnRight(0.7, .1) else: forward(1)
Wander - Avoiding Obstacles # uses IR sensors to wander while # avoiding obstacles def wander(seconds): start = currentTime() while (currentTime() - start < seconds): r = getObstacle(“right”) l = getObstacle(“left”) c = getObstacle(“center”) if r > 0: beep(.2, 440) backward(1, .1) turnLeft(0.7, .1) elif l > 0 or c > 0: beep(.2, 880) backward(1, .1) turnRight(0.7, .1) else: forward(1) • Use the IR sensors on the robot to avoid obstacles • Add stall sensor?
Wander - Avoiding Obstacles # uses IR sensors to wander while # avoiding obstacles def wander(seconds): start = currentTime() while (currentTime() - start < seconds): r = getObstacle(“right”) l = getObstacle(“left”) c = getObstacle(“center”) if r > 0 or getStall(): beep(.2, 440) backward(1, .1) turnLeft(0.7, .1) elif l > 0 or c > 0: beep(.2, 880) backward(1, .1) turnRight(0.7, .1) else: forward(1) • Use the IR sensors and stall sensor on the robot to avoid obstacles