Class 12 – Monday, September 23
In a loop, a loop — Nesting, but not like a bird — Repeating again
Look out my window — And see random acts of grace — By everyone
Look both ways
Agenda
- Extend
print()
function functionality
- Introduce randomness
- Doing loop the loops
- Introduce pillow.
For the fun of it
- Reveal one of your super power(s).
- Share a selfie.
To do list
- Review class artifacts.
- Complete homework.
- Install pillow.
Downloads
- Program thats_printastic.py
- Program so_random.py
- Program and_the_winner_is.py
- Program nested_looper.py
- Program dataset.py
Optional printing
-
Program thats_printastic.py
- Introduces the optional closing parameter
end="..."
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-
saippuakivikauppias
Introducing module random
- Program so_random.py
- Demonstrates some simple usage of
random
module functionrandrange()
- Some program runs
Enter two integers: 10 100
Four random values from interval [ 0, 10 ): 7 4 3 5
Four random values from interval [ 0, 100 ): 15 28 64 87
Enter four integers: 10 20 75 100
Four random values from interval [ 0, 10 ): 4 4 8 8
Four random values from interval [ 0, 100 ): 22 64 2 80
Enter two integers: 10 100
Four random values from interval [ 0, 10 ): 1 2 8 6
Four random values from interval [ 0, 100 ): 57 63 81 18
Enter four integers: 10 20 75 100
Four random values from interval [ 10 , 20 ): 11 11 19 15
Four random values from interval [ 75 , 100 ): 89 85 76 99
Enter two integers: 10 100
Four random values from interval [ 0, 10 ): 3 5 3 6
Four random values from interval [ 0, 100 ): 29 12 32 5
Enter four integers: 10 20 75 100
Four random values from interval [ 10 , 20 ): 13 14 16 19
Four random values from interval [ 75 , 100 ): 92 98 75 75
Enter two integers: 2 10
Four random values from interval [ 0, 2 ): 1 0 0 1
Four random values from interval [ 0, 10 ): 6 6 2 8
Enter four integers: 0 8 31 39
Four random values from interval [ 0 , 8 ): 7 2 1 3
Four random values from interval [ 31 , 39 ): 37 36 32 37
- Program and_the_winner_is.py
- Demonstrates some simple usage of
random
module functionrandrange()
- Some program runs
Enter a string: abcdefghij
Four random characters from string abcdefghij : e a j d
Enter some words: th fo ju ov th la lo
Four random words from list ['th', 'fo', 'ju', 'ov', 'th', 'la', 'ol'] : ov lo la ov
Enter a string: abcdefghij
Four random characters from string abcdefghij : a i b h
Enter some words: th fo ju ov th la lo
Four random words from list ['th', 'fo', 'ju', 'ov', 'th', 'la', 'ol'] : th fo th ju
Enter a string: simultaneously
Four random characters from string simultaneously : i y m t
Enter some words: 31 41 597 2 71
Four random words from list ['31', '41', '597', '2', '71'] : 71 597 31 71
Nested looping
- Program nested_looper.py
- Introduces nested looping. Where for a user-supplied
n
, the program loopsn
times and on thei
th iteration prints a line of the form
i: 0 1 2 ... i
- Two sample program runs
Enter number: 5
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
Enter number: 10
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
row 6: 0 1 2 3 4 5 6
row 7: 0 1 2 3 4 5 6 7
row 8: 0 1 2 3 4 5 6 7 8
row 9: 0 1 2 3 4 5 6 7 8 9
Examining a list whose elements are lists
- Such lists are often called tables or datasets
-
Program dataset.py
- Demonstrates looping through a two-dimensional in different ways
Program run
[['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', 'K', 'L']]
['A', 'B', 'C']
['D', 'E', 'F']
['G', 'H', 'I']
['J', 'K', 'L']
row 0: ['A', 'B', 'C']
row 1: ['D', 'E', 'F']
row 2: ['G', 'H', 'I']
row 3: ['J', 'K', 'L']
row 0 : ['A', 'B', 'C']
column 0 of row 0 : A
column 1 of row 0 : B
column 2 of row 0 : C
row 1 : ['D', 'E', 'F']
column 0 of row 1 : D
column 1 of row 1 : E
column 2 of row 1 : F
row 2 : ['G', 'H', 'I']
column 0 of row 2 : G
column 1 of row 2 : H
column 2 of row 2 : I
row 3 : ['J', 'K', 'L']
column 0 of row 3 : J
column 1 of row 3 : K
column 2 of row 3 : L
A B C
D E F
G H I
J K L
© 2019 Jim Cohoon | Resources from previous semesters are available. |