Turtle Graphics Victor Norman CS104 Calvin College Reading Quiz • Counts toward your grade. 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). Clicker Question 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) for Loop Examples for val in [0, 1, 2, 3, 4, 5]: turt.forward(50) turt.left(60) total = 0 for val in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]: total = total + val print(total) total = 0 for val in range(1, 21): total = total + val print(total) 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 omit start uses 0. E.g., range(10) • Can omit step uses 1. E.g., range(3, 7) 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. Clicker Questions 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 import (cont) • If you want to pull something (everything) out of the “box”, you can do this from math import sin from math import * 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(). Assignments • Do the few TuringsCraft questions before lab on Thursday. • (Optionally) Practice your typing skills: play.typeracer.com or some other site…