Reading Questions
Quiz
Practice Session
- Sunday - 6PM - Trexler 363 (i.e. our classroom)
Accumulating
- The accumulator pattern is updating variables inside of a loop
- Recall the square spiral program - delta_dist = 10 dist = delta_dist turtle.forward(dist) turtle.left(90) dist = dist + delta_dist turtle.forward(dist) turtle.left(90) dist = dist + delta_dist turtle.forward(dist) turtle.left(90) dist = dist + delta_dist turtle.forward(dist) turtle.left(90) dist = dist + delta_dist
- That’s a lot of copying and pasting that would be better in a loop - delta_dist = 10 dist = delta_dist for i in range(10): turtle.forward(dist) turtle.left(90) dist = dist + delta_dist
- It is also possible to do it without the accumulator - for i in range(10): turtle.forward(i * 10 + 10) turtle.left(90)
- It could even be done using range - for i in range(10, 100, 10): turtle.forward(i) turtle.left(90)
- Note, however, that not all problems can be solved with range
- Range does not work for multiple values or values which change non-linearly
- Also note, not all problems can be solved with equations using the loop variable
- Loop variable equations do not work if the value is not predicable (i.e. user input or randomization)