Slides - GitHub Pages

Repetition
with Turtle & for Loops
COSC 101: Intro to Computing I
February 7, 2017
Spring 2017
COSC 101: Introduction to Computing I
1
Warmup Handout
Spring 2017
COSC 101: Introduction to Computing I
2
Turtles
Spring 2017
COSC 101: Introduction to Computing I
3
Write a program that uses a turtle to draw this shape:
Spring 2017
COSC 101: Introduction to Computing I
4
Iteration
Spring 2017
COSC 101: Introduction to Computing I
5
for loops
name = "madeline"
•
loop
Why use a loop?
variable for i in [1, 2, 3]:
print(letter)
What is a loop variable?
loop
body print('.')
What is the loop body?
•
What does our loop iterate through?
•
What is a list?
•
What is a range?
•
•
{
Spring 2017
COSC 101: Introduction to Computing I
6
for loop
execution
Spring 2017
COSC 101: Introduction to Computing I
7
import turtle
window = turtle.Screen() What will happen when
the code on the left is
lil_t = turtle.Turtle()
executed?
lil_t.forward(250)
lil_t.left(90)
lil_t.forward(250)
lil_t.left(90)
Write a script that draws
the same shape using a
for loop.
lil_t.forward(250)
lil_t.left(90)
lil_t.forward(250)
window.exitonclick()
Spring 2017
COSC 101: Introduction to Computing I
8
import turtle
window = turtle.Screen() What will happen when
the code on the left is
lil_t = turtle.Turtle()
executed?
lil_t.forward(350)
lil_t.left(90)
lil_t.forward(200)
lil_t.left(90)
lil_t.forward(350)
Write a script that draws
this same shape using
a for loop with only two
lines in the loop body.
lil_t.left(90)
lil_t.forward(200)
window.exitonclick()
Spring 2017
COSC 101: Introduction to Computing I
9
Accumulator Pattern
A strategy for completing a task using a for loop
1. Answer these questions:
A. What do you want to compute?
B. What is its type?
C. How can we build it one piece at a time?
2. Initialize accumulator variable (use the right type).
3. Update the accumulator variable time through the loop.
4. After the loop, the accumulator variable is result.
Spring 2017
COSC 101: Introduction to Computing I
10
Write a short program that asks the user for their name and
then prints a copy of the name that is both duplicated and
reversed. For example, if the user enters "bud", it would
print "dduubb". # solution on next slide
Spring 2017
COSC 101: Introduction to Computing I
11
Write a short program that asks the user for their name and
then prints a copy of the name that is both duplicated and
reversed. For example, if the user enters "bud", it would
print "dduubb". name = input("Your name: ")
# initialize accumulator variable as empty string
name2 = ''
for letter in name:
# update accumulator
name2 = letter + letter + name2
# when loop is over, name2 has final result
print("Your name reversed and duplicated", name2)
Spring 2017
COSC 101: Introduction to Computing I
12
Same idea but print a palindrome version of name
(palindromes read the same forwards and backwards). For
example, if the user enters "bud", it would print "buddub". # solution on next slide
Spring 2017
COSC 101: Introduction to Computing I
13
Same idea but print a palindrome version of name
(palindromes read the same forwards and backwards). For
example, if the user enters "bud", it would print "buddub". name = input("Your name: ")
# initialize accumulator variable to be
name2 = ''
for letter in name:
# update accumulator
name2 = letter name2
name2 = name + name2
# when loop is over, name2 has final result
print ("Your palindrome name is", name2)
Spring 2017
COSC 101: Introduction to Computing I
14