Learning Objectives
Through readings, lecture and labs, it is expected that you are doing work and seeking help to keep pace with the below objectives:
- Variables, input, output, assignment.
You should be achieving much greater competance with this topic's objectives from last week.
- Definite (
for
) loops - You should be comfortable with:
- Using for loops to execute blocks of code repeatedly
- Tracing the sequence of values taken by a loop counter variable
- Using range to manipulate a loop counter
- Accumulator pattern
- You should be able to:
- Trace the execution of code that uses accumulation
- Recognize simple examples of problems that use this pattern
- Write code to solve these problems
- Our graphics library
- You should be comfortable with:
- Writing programs that display a graphics window.
- Drawing colored circles and rectangles in the graphics window.
- Calculating the desired positions of circles and rectangles.
Required Reading
Supplemental Materials
We all need alternative presentations of the concepts from time to time. The following resources are optional. If something seems unclear after class, or the pratice questions seem hard for you, then explore these topics more using the resources below.
- Definite (
for
) loops -
- CS Circles lesson on loops
- Zelle, Sections 2.6, 2.7
- Accumulator pattern
-
- Video: using a loop to keep a running total (i.e. an accumulation)
- Zelle, Sections 2.7, 3.3, 8.1
- Miller and Ranum, Newton's Method. This is a use of loops for a more complex application, called Newton's method. This is not a simple example, but its a good test of your understanding if you are feeling comfortable with the simpler examples we've seen.
- Our graphics library
-
- CS115 Graphics Tutorial
- Zelle, Sections 4.1–4.4
Practice
For all these exercises, I recommend you try them on paper and then test them out with PythonTutor.Exercise 1
Write what this code produces, exactly. Be very careful with whitespace and the newlines.
a = 11 b = 4 c = a // b d = a % b print(a,b, sep='/', end=" => ") print(c,d, sep=' remainder ', end="?!")
Exercise 2
Write what this code produces, exactly. Be very careful with whitespace and the newlines.
a = 10 b = 7 print(a % b, a+b, a*b, "a-b", "10"+"7", sep="\n")
Exercise 3
Write what this code produces, exactly. Be very careful with whitespace and the newlines.
seaw = 'SeaWolves' print('Go', seaw, end='!\n!') print(seaw + ' Rock')
Exercise 4
Write what this code produces, exactly. Be very careful with whitespace and the newlines.
k = 0 for i in range (1 , 10, 2): print(i) k = k + i print (k)
Exercise 5
Write a piece of code that produces the following input/output behavior. Text that is italics and underlined are user input.
Enter an integer: 100 Sum of squares of numbers 1--100 is: 338350Here is a sample answer, when you think you have solved it. (Don't cheat yourself and look early).