Sei sulla pagina 1di 23

Python and

Scribbler Robots
Hélène Martin
Puget Sound CSTA
April 24th, 2010

Special thanks to Marty Stepp for some formatting and content


Friday, April 23, 2010
Hélène Martin

Bachelor’s in CS from UW
Career and Technical Ed through South Seattle
Teach at comprehensive public high school
Five periods of computer science
- 2x Exploring Computer Science
- 2x Creative Computing
- 1x AP Computer Science

http://garfieldcs.com

Friday, April 23, 2010


Game Plan

Why bother with Python


Python language basics
Why bother with Scribblers
myro package basics
Play!

Friday, April 23, 2010


Why Teach Python?

Light-weight syntax
Scalable (small hacks to GUI apps)
Lots of great libraries
- myro (today)
- pygame
- scipy

Interpreted
Permissive
Used increasingly in college courses

Friday, April 23, 2010


Why Not Teach Python?

Permissive
Dynamically typed (values have types, but not
variables)
Too many ways to skin a single cat!
Not used much in corporate world
- Though YouTube, Google, Yahoo!, NASA make some use of it

Friday, April 23, 2010


Getting Python

Free at http://python.org
Generally pre-installed on OS X or Linux
Includes Idle, an IDE

Friday, April 23, 2010


Interacting With Python

Programs saved with .py extensions


Commands interpreted straight in shell
Students can get confused about the two!!

Friday, April 23, 2010


Print Function

Simple! (no System.out.yuck)


Can be called with parameters of different types
Concatenation shows ugliness of dynamic types

Print can display different types


1 print("Hello, world!")
2 print()
3 print(25)
4 print("You have " + str(dollars) + " dollars")

Friday, April 23, 2010


Python Operations

Arithmetic:
- ** (exponent)
- * (multiplication) / (division) % (modulus)
- + (addition) - (subtraction)

Boolean
- == != < <= > >=
- not
- and or

Common Types
- int, float, bool, str

Friday, April 23, 2010


White Space

Indentation indicates blocks


Super offensive at first glance, but one gets over it
- it’s NOT like shell scripting!

Colon indicates containers, everything indented


under is governed by that container

Indentation matters
1 for i in range(3):
2 print("Hello") # repeated
3 print(25) # repeated
4 print("Hi") # not repeated

Friday, April 23, 2010


Iteration

Definite loop
Basic for loop structure
1 for i in range(<times>):
2 <statements>
3 <statements>
4 print("Hi") # not repeated

Indefinite loop
While loop strcture
1 while(<condition>):
2 <statements>
3 <statements>
4 print("Hi") # not repeated

Friday, April 23, 2010


Conditionals

Conditional structure
1 if(<condition>):
2 <statements>
3 elif(<condition>):
4 <statements>
5 else:
6 <statements>

Friday, April 23, 2010


Functions

Definition
The def keyword is used for defining functions
1 def hello(<param1>, <param2>):
2 print("Hello, world!“)
3
4 hello()
5 hello()

Calling
- A common error is to forget parentheses after the call

Friday, April 23, 2010


External Libraries

Import modules to get access to more functions

Using random numbers


1 from random import *
2
3 die1 = randint(1, 6)
4 die2 = randint(1, 6)

Friday, April 23, 2010


Python Resources

http://python.org/
http://diveintopython.org
http://openbookproject.net//thinkCSpy/

Friday, April 23, 2010


Scribblers

Friday, April 23, 2010


Why Teach With Scribblers

Visual and tactile feedback is motivating


Opportunity to discuss robotics
“Cool” factor

Indestructible and no assembly necessary


Uses a real language

Friday, April 23, 2010


Why Not Teach With Scribblers

Flakiness leads to frustration


Too many things going on
Limited interesting applications

Friday, April 23, 2010


My Successes

Python as primary language in Creative


Computing
- 120 students so far

2 week unit on Scribblers for Creative


- Students already know a lot of Python

Great projects: object detection, “piano”,


obstacle course completion

Friday, April 23, 2010


My Challenges

Python as quick introduction to “real” language


after Scratch in Exploring Computer Science
1 week intro to Scribblers for Exploring
Ended unit early because students just weren’t
getting it and flakiness was irritating!

Friday, April 23, 2010


myro

Python library gives access to Scribbler functions

Code needed to get going with myro


1 from myro import *
2 init(<comXX>)

Friday, April 23, 2010


A Sample Program
def flashLight(times):
for i in range(times):
setLED("back",1)
wait(0.55)
setLED("back",0)
wait(0.25)

def senseLoop():
# getLight() is a function that gives back three values
# Here we save them in three variables.
rightSensor, centerSensor, leftSensor = getLight()

# Print out all three sensor values


print "Light Sensor Values:", rightSensor, centerSensor, leftSensor;

# If the right sensor is covered


if rightSensor > 2000:
# You can write a song using note names and beat lengths
playSong(makeSong("c 1; g 1/4; a 1/2; e 3/4;") )
# Make the back LED flash
flashLight(5)

if centerSensor > 5000:


print "BAGELS BAGELS GGRRAA" # don't ask; it's late
wait(0.25)
beep(0.25,440,880)

if leftSensor > 2000:


turnLeft(1,0.5)

# Continue sensing for 20 seconds


while(timeRemaining(20)):
senseLoop()

Friday, April 23, 2010


Play Time!

Friday, April 23, 2010

Potrebbero piacerti anche