Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero.

Similar presentations


Presentation on theme: "Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero."— Presentation transcript:

1 Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

2 Overview Sensory definitions Randomness Asking questions General while loop Conditions Sensing functions If statement

3 Sensory Definitions External sensors - exteroceptors ▫Sight, sound ▫IR sensors, camera Internal sensors - interoceptors ▫Position of self, pain ▫time, stall, battery level

4 Randomness Random numbers are useful ▫Real life has randomness ▫from random import * random() ▫Returns a value between 0.0 and 1.0 randint(A, B) ▫Returns a value between A and B

5 Asking Questions Graphically presents the user with a question askQuestion(“Do you like tacos?”) Returns the value of the button label Override the amount of buttons and default labels with a list askQuestion(“What is your favorite taco”, [“Lengua”, “Tripa”, “Pastor”, “Carnita”])

6 Time timeRemaining( ) wait( ) currentTime() ▫Returns the number of seconds elapsed since some predefined time

7 Moving Forward forward(1.0, 3.0) forward(1.0) wait(3.0) stop() while timeRemaining(3.0): forward(1.0) stop()

8 General While Loops startTime = currentTime() while (currentTime() – startTime) < 3.0: forward(1.0) stop() while :

9 Conditions Expressions that evaluate to True or False Comparison (relational) operators ▫< less than ▫> greater than ▫<= less than or equal ▫>= greater than or equal ▫== equal ▫!= not equal

10 Conditions Examples ▫42 > 23 Logical (boolean) operations ▫and, or, not Examples ▫(6 > 7) or (3 > 2) ▫(a == “hello”) and (count > 1) 23 == 23 “Hello” < “hello”

11 Boolean Algebra (A or True) will always be True (not (not A)) will just be A (A or (B and C)) is the same as ((A or B) and (A or C) (A and (B or C)) is the same as ((A and B) or (A and C)) (not (A or B)) is the same as ((not A) and (not B)) (not (A and B)) is the same as ((not A) or (not B))

12 Sensing Functions getStall() ▫Returns True if the robot has stalled, False otherwise getBattery() ▫Returns current voltage being supplied by battery while not getStall(): forward(1.0) stop() speak(“Muhaha! I meant to do that!”)

13 If statements if : if a < 5: print “Buy more Doritos” if not tired: print “Play more video games”

14 If-Else Statement if : else: if tired: goToSleep() else: cleanKitchen()


Download ppt "Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero."

Similar presentations


Ads by Google