Foldit
Reading Questions
Quiz
Range
- We’ve already seen range with single input
- If we want different values for i, can write expressions to compute them
- For example if want to start at 1, add 1 to i
- There is an easier way, range has optional arguments
- If there are two arguments they are start and stop
- If there are three arguments they are start, stop, and step
Loop Patterns
- There are two different patterns to write the same program using loops
For example a program that prints the positive even numbers less than 20 could be written using range to change the loop variable
for i in range(2, 20, 2): print(i)
Or, it could be written with an equation that uses the loop variable
for i in range(1, 10, 1): print(i * 2)
- Using the range function pattern is often easier
- But using the loop variable equation pattern is more powerful
- It can be used to solve problems that the range function pattern can not
For example, a series of numbers that does not increase the same amount every iteration of the loop such as the powers of two (2, 4, 8, 16, …)
for i in range(1, 20, 1): print(2 ** i)
Loop Game
- The first activity for today is a game
- The game helps you practice these two patterns
- Complete the code to produce the desired graph
- Note, the ** operator does not exist, use *
New Lines
Can specify that a print statement doesn’t end in a new line
print("something", end=" ") print("something...")
- Whatever is inside of the quotes after
end=
will be used instead of a new line This is useful if you want to put everything a for loop prints on one line
for i in range(3): print(i, end=" ")
- Note, this will leave a space at the end of the last print statement and no new line
- Can get a new line with
print()
after the loop How can you get rid of the extra space?
Range Peculiarities
- What happens if you use a floating point number?
- What happens if you use a floating point number that represents an integer?
- What happens if you flip the start and stop values?
- What happens if you start and stop at the same value?
- What happens if you change the loop varible, inside of the loop?
More Turtle
goto
tell the turtle to go to an x, y coordinate- Where is the orgin?
- How big is the coordinate plane?
setheading
tell the turtle to turn to an orientation- useful when turtle code is mixing forward and goto commands