120 likes | 262 Views
Introduction to Computer Science – Chapter 2. CSc 2010 Spring 2011 Marco Valero. Overview. Python Tutorial Scribbler movements Defining new commands Adding parameters to commands Saving new commands in modules Functions as building blocks. Python Tutorial. Command Prompt Hello World!
E N D
Introduction to Computer Science – Chapter 2 CSc 2010 Spring 2011 Marco Valero
Overview • Python Tutorial • Scribbler movements • Defining new commands • Adding parameters to commands • Saving new commands in modules • Functions as building blocks
Python Tutorial • Command Prompt • Hello World! • Giving Commands • Functions • Parameters • Module • import os,sys • sys.path.append('/path/to/module')
Movements • motor(LEFT, RIGHT) • LEFT, RIGHT = [-1.0, 1.0] • forward(SPEED) • SPEED = [-1.0,1.0] • backward(SPEED) • turnLeft(SPEED) • turnRight(SPEED) • stop()
Movements • forward(SPEED, SECONDS) • SECONDS = [0.0, …] • backward(SPEED , SECONDS) • turnLeft(SPEED , SECONDS) • turnRight(SPEED , SECONDS)
Defining new commands def yoyo(): forward(1) backward(1) stop() def yoyo(): forward(1, 1) backward(1,1)
Defining new commands def yoyo(): forward(1) wait(1) backward(1) wait(1) stop()
Adding parameters to commands def yoyo(speed, waitTime): forward(speed) wait(waitTime) backward(speed) wait(waitTime) stop() • speed, waitTime are our parameters
Saving new commands in modules # File: moves.py # Purpose: Two useful robot commands to try out as a module. # First import myro and connect to the robot from myro import * init() # Define the new functions... def yoyo(speed, waitTime): forward(speed) wait(waitTime) backward(speed) wait(waitTime) stop() def wiggle(speed, waitTime): rotate(-speed) wait(waitTime) rotate(speed) wait(waitTime) stop()
Functions as building blocks def dance(): yoyo(0.5,0.5) yoyo(0.5,0.5) wiggle(0.5,1) wiggle(0.5,1)