Class 12 – Monday, September 21
Building and examining lists
An optimist thinks — Elevator close button — Really does something
Look both ways
Agenda
- Extend
print()
function functionality
- Introduce nested loops
- Program thats_printastic.py
- Program c_looper.py
- Program nested_looper.py
- Program rc_looper.py
To do list
- Reveal one of your super power(s)
- Share a selfie
- Review class artifacts.
- Complete the homework
Program thats_printastic.py
— introduce print()
optional closing parameter end="..."
Task
- The optional
end="..."
parameter for aprint()
function allows changing from its default action of starting the next print on a new line.
Program run
Wa-Hoo-Wa
Rah-Rah-Rah
Wa-Hoo-Wa!Rah-Rah-Rah
Wa-Hoo-Wa! Rah-Rah-Rah
t-a-t-t-a-r-r-a-t-t-a-t-
s a i p p u a k i v i k a u p p i a s
saippuakivikauppias
Program c_looper.py
Task
- For a user-supplied
n
, prints a line of the form
row : 0 1 2 ... n-1
Some program runs
Enter number of columns: 5
row : 0 1 2 3 4
Enter number of columns: 12
row : 0 1 2 3 4 5 6 7 8 9 10 11
Nested looping — program nested_looper.py
Task
- Introduces nested looping. Where for a user-supplied
n
, the program loopsn
times and on ther
th iteration prints a line of the form
row r : 0 1 2 ... r
Some program runs
Enter number of rows: 4
row 0 : 0
row 1 : 0 1
row 2 : 0 1 2
row 3 : 0 1 2 3
Enter number of rows: 6
row 0 : 0
row 1 : 0 1
row 2 : 0 1 2
row 3 : 0 1 2 3
row 4 : 0 1 2 3 4
row 5 : 0 1 2 3 4 5
Nested looping — program rc_looper.py
Task
- Further introduces nested looping. Where for a user-supplied
n
, the program loopsn
times and on ther
th iteration prints a line of the form
r+0 r+1 ... r+n-1
Some program runs
Enter number of rows: 5
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
Enter number of rows: 10
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
© 2020 Jim Cohoon | Resources from previous semesters are available. |