introduction to computer science – chapter 4 csc 2010 spring 2011 marco valero

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

Upload: brendan-sharp

Post on 04-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

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

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

Page 2: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

Overview

• Sensory definitions

• Randomness

• Asking questions

• General while loop

• Conditions

• Sensing functions

• If statement

Page 3: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

Sensory Definitions

•External sensors - exteroceptors

▫Sight, sound

▫IR sensors, camera

•Internal sensors - interoceptors

▫Position of self, pain

▫time, stall, battery level

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

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

Page 5: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

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”])

Page 6: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

Time

•timeRemaining(<duration>)

•wait(<count>)

•currentTime()

▫Returns the number of seconds elapsed

since some predefined time

Page 7: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

Moving Forward

forward(1.0, 3.0)

forward(1.0)wait(3.0)stop()

while timeRemaining(3.0):forward(1.0)

stop()

Page 8: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

General While Loops

startTime = currentTime()while (currentTime() – startTime) < 3.0:

forward(1.0)stop()

while <some condition is true>:<do something>

Page 9: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

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

Page 10: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

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”

Page 11: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

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

Page 12: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

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

Page 13: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

If statements

if <condition>:<do something>

if a < 5:print “Buy more Doritos”

if not tired:print “Play more video games”

Page 14: Introduction to Computer Science – Chapter 4 CSc 2010 Spring 2011 Marco Valero

If-Else Statement

if <condition>:<do something>

else:<do something>

if tired:goToSleep()

else:cleanKitchen()