Class 12 – Monday, February 14
Loops and Indexing
Look both ways
Agenda
- Indexing a list within a loop
- Loop chrestomathics
- Extend
print()
function functionality
Downloads
- Submitted Classwork: Program half_scores.py
- Determine average score for first or second half of semester.
- Program on_average.py
- Determine average word length
- Program thats_printastic.py
- Introduce
print()
optional closing parameterend="..."
- Program c_looper.py
- Foreshadow nested looping
- Program secret_decoder.py
- Chrestomathics — display a secret message using a code phrase and indices
- Program going_rowing.py
- Introduce datasets
- Demonstrate looping through a dataset
Program half_scores.py
-
Problem
- The program takes 8 numbers of input, which represent a student's quiz scores in a course. Then, the program calculates the student's average score for the first 4 quizzes, and also calculates their average for the last four quizzes, and prints both averages out.
-
Realizations
- To compute the average of both halves of quizzes, we cannot as easily loop through the full list of numbers.
- If you come up with an answer with no loops, try to come up with a second answer that incorporates loops.
-
One Possible Algorithm
- Prompt and get text (i.e., a bunch of numbers).
- Split input into a list of strings.
- Create a list to store the ints for the upcoming loop. (accumulator)
- Process each string in the string list: (loop)
- Cast string to int, then append it to the int list.
- Create a running total variable, which we'll add each of the first four tests to (accumulator)
- Process each number in the first half of the list (indices 0 through 3): (loop)
- Add that number to the running total of the first quiz scores.
- Compute average by dividing by '4.0'.
- Print average.
- **Do everything again for the second half of the quiz scores.
- Create a running total variable, which we'll add each of the last four tests to (accumulator)
- Process each number in the second half of the list (indices 4 through 7): (loop)
- Add that number to the running total of the last quiz scores.
- Compute average by dividing by '4.0'.
- Print out both averages.
-
A sample program run
Enter scores: 100 90 85 60 77 44 99 100
First half score: 83.75
Second half score: 80.0
-
Please make sure to submit your completed program! :)
Program on_average.py
-
Problem
- Examines a list of user-supplied words and reports determine average word length.
-
Realizations
- Because the number of words is not know when the program is being written, a loop is needed to process the words.
- To compute the average word length, need to know the number of words and the sum of the word lengths.
- The loop needs to two things for each word: compute its length and add its length to a running total.
- The running total needs to be initialized before the loop.
-
Algorithm
- Prompt and get text (i.e., a bunch of words).
- Split input into a word list.
- Echo word list to user.
- Compute the length of the list.
- Initialize running total of word lengths to 0.
- Process each word in the word list:
- Compute length of the current word of interest.
- Add that length to running total of the word lengths.
- Compute average from the word lengths total and the number of words.
- Print average.
-
Two program runs
Enter text: over the hill and through the dale
words = ['over', 'the', 'hill', 'and', 'through', 'the', 'dale']
Average word length: 4.0
Enter text: row row row your boat
words = ['row', 'row', 'row', 'your', 'boat']
Average word length: 3.4
Program thats_printastic.py
- Introduce
print()
optional closing parameterend="..."
- 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
s-a-i-p-p-u-a-k-i-v-i-k-a-u-p-p-i-a-s-.
s a i p p u a k i v i k a u p p i a s .
saippuakivikauppias.
Program c_looper.py
- Foreshadow nested looping
- 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
Program dataset_intro.py
- Demonstrate looping through a dataset in different ways
-
Program run
table: [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', 'K', 'L', 'M']]
the table has 4 rows
row ['A', 'B', 'C'] has 3 columns
row ['D', 'E', 'F'] has 3 columns
row ['G', 'H', 'I'] has 3 columns
row ['J', 'K', 'L', 'M'] has 4 columns
row 0 : ['A', 'B', 'C']
row 1 : ['D', 'E', 'F']
row 2 : ['G', 'H', 'I']
row 3 : ['J', 'K', 'L', 'M']
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |