turtle graphics victor norman cs104 calvin college

12
Turtle Graphics Victor Norman CS104 Calvin College

Upload: samson-young

Post on 27-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Turtle Graphics Victor Norman CS104 Calvin College

Turtle Graphics

Victor NormanCS104

Calvin College

Page 2: Turtle Graphics Victor Norman CS104 Calvin College

Reading Quiz

• Counts toward your grade.

Page 3: Turtle Graphics Victor Norman CS104 Calvin College

Quick Introduction to Objects

• You have to instantiate the object, making a variable refer to it:– turt = turtle.Turtle()

• You call (or "invoke") methods on it:– turt.forward(10)– turt.doSomething(33, 22, 11)

• It is like asking the object to do something to itself: give me back a value or move yourself, or draw something, or change a value.

• The object stores its own state (or characteristics, properties, or attributes).

Page 4: Turtle Graphics Victor Norman CS104 Calvin College

Clicker Question

Page 5: Turtle Graphics Victor Norman CS104 Calvin College

for Loop Syntax

• Pattern:for <var> in <sequence>:

<body> # multiple statements

• Examples:for aVal in [3, 11, 22, 0, -3]:

print(aVal)for aVal in [“I’m”, “a”, “lumberjack”, “and”,

“I’m”, “ok”, 8, math.pi]:

print(aVal)

Page 6: Turtle Graphics Victor Norman CS104 Calvin College

for Loop Examplesfor val in [0, 1, 2, 3, 4, 5]:

turt.forward(50)turt.left(60)

total = 0for val in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]:

total = total + valprint(total)

total = 0for val in range(1, 21):

total = total + valprint(total)

Page 7: Turtle Graphics Victor Norman CS104 Calvin College

range(start, stop, step)

• Built-in function• Generates list of integers starting at start,

going up to (but not including) stop, by increments of step.

• Can skip start uses 0. E.g., range(10)• Can skip step uses 1. E.g., range(3, 7)

Page 8: Turtle Graphics Victor Norman CS104 Calvin College

range() Examples

range(30)• generates list of numbers 0, 1, 2, …, 29.range(2, 30)• generates list of numbers 2, 3, 4, 5, …, 29.range(-3, 3, 2)• generates list of numbers -3, -1, 1.range(20, 0, -1)• generates list of numbers 20, 19, 18, 17, …, 2, 1.

Page 9: Turtle Graphics Victor Norman CS104 Calvin College

Clicker Questions

Page 10: Turtle Graphics Victor Norman CS104 Calvin College

import statement

• import pulls a module in to your program.• A module is a file with code and variables in it.– math module has sin(), cos(), round(), pi, e, etc.– myro has init(), forward(), backward(), etc.

• import loads the module in, but all the stuff in it is still in it. Access it via the “dot operator”.– import loads the “box” in but your code still has to

reach into the box to get at the functions, variables, etc.• e.g., math.sin(), math.pi

Page 11: Turtle Graphics Victor Norman CS104 Calvin College

import (cont)

• If you want to pull something (everything) out of the “box”, you can do this from math import *from math import sin

• Then you don’t need to tell your code to reach into the module to get the function/variable.– e.g., can use sin() instead of math.sin() – init() instead of myro.init().

Page 12: Turtle Graphics Victor Norman CS104 Calvin College

Assignments

• Do the few TuringsCraft questions before lab on Thursday.