Using Loops + Animation
CS 110
1
2
if condition:
statement 1
statement 2
...
CONDITION�Expression that evaluates to True or False.
Indented BLOCK�If the condition evaluates to True, the block executes. Otherwise, the block is skipped.
else:
statement 1
statement 2
...
3
if condition:
statement 1
statement 2
...
CONDITION�Expression that evaluates to True or False.
Indented BLOCK�If the condition evaluates to True, the block executes. Otherwise, the block is skipped.
elif other_condition:
…
else:
statement 1
statement 2
...
for thing in my_sequence:
statements…
4
Keyword for marks the start of for loop
An arbitrary variable name to store each item in the list as we go through it
Any sequence �(tuple, list, set, string)
loop body has 1 or more statements, which have same indentation level
For Loop: “Keep retrieving the next item in the sequence, starting from the beginning, until you reach the end”. Visualize for loop.
Keyword in tells Python we're ready to specify our sequence of data
5
while condition:
statement 1
statement 2
...
CONDITION�Boolean expression that evaluates to True or False.
INDENTED BLOCK�While the condition evaluates to True, the block will continue to execute over and over. Otherwise, the block is skipped.
6
Animation!
7
All of animation is just a fancy flipbook
8
All of animation is just a fancy flipbook
9
Step 1. Draw a picture
Step 2. Go to new page
Step 3. Draw a slightly different picture
Step 4. Go to a new page
Step 5. Draw a slightly different picture
Step 6. …
All of animation is just a fancy flipbook
10
Step 1. Draw a picture
Step 2. Go to new page (or erase)
Step 3. Draw a slightly different picture
Step 4. Go to a new page (or erase)
Step 5. Draw a slightly different picture
Step 6. …
If you had a list of pictures…
11
for picture in list_of_pictures:
erase(picture)
draw(picture)
wait(1)
If you wanted an infinite loop…
12
ticks = 0
while True:
draw(picture based on ticks)
wait(1)
erase(picture)
ticks += 1