1 / 25

Randomness and Repetition

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

admon
Télécharger la présentation

Randomness and Repetition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Randomness and Repetition Dr. Paige H. Meeker Notes from various text books, IPRE slides

  2. 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!

  3. 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.

  4. 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.

  5. 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.

  6. 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”]

  7. 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

  8. 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)

  9. 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”]

  10. 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

  11. While… • While statements are a looping construct that executes while some condition (or “Boolean expression”) is true. while BOOLEAN EXPRESSION : statement statement …. statement statement

  12. 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

  13. What does this do? i = 1 while i < 4: beep(i,440) i = i + 1

  14. What does this do? i = 1 while i > 4: beep(i,440) i = i + 1

  15. What does this do? i = 1 while i < 4: beep(i,440) i = i - 1

  16. 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.

  17. 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!”)

  18. 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!”)

  19. 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()

  20. 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

  21. 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()

  22. 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?

  23. 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)

  24. 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?

  25. 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

More Related