100 likes | 441 Views
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)
E N D
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).
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”]: 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 skip start uses 0. E.g., range(10, 2) • Can skip step uses 1. E.g., range(10)
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.
Assignments • Do the few TuringsCraft questions before lab on Thursday.