Class 9 – Monday, September 14
Do that again
Did you not expect – The repetition to come – Did you not expect
Look both ways
Agenda
- Looping
- List chrestomathics
Downloads
- Program kinda_stringy.py
- Program wordy.py
- Program on_average.py
- Program again_and_again.py
- Program m_and_ns.py
- Program n_and_ms.py
- Program inty.py
To do list
- Review class artifacts.
- Read the epistle on lists.
- Check out my documentation for the
list
module.
Program kinda_stringy.py
Task
- Prompt, get, and individually print out the letters of a user-supplied three-character string.
- Prompt, get, and individually print out the letters of a user-supplied character string
Realization
- A loop is not necessary to handle the first input, as the number of characters to process is known when the program is being written.
- A loop is necessary to handle the second input, as the number of characters to process is not known when the program is being written.
- The loop to individually process the characters of the second input needs to done one thing each character, print it out.
Two possible program runs
Enter a three-letter string: abc
a
b
c
Enter a string: am too
a
m
t
o
o
Enter a three-letter string: def
d
e
f
Enter a string: are not
a
r
e
n
o
t
Program wordy.py
Task
- Prompt and get text.
- Echo text to user.
- Echo word list to user.
- Report the length of the words one per line.
Realizations
- Because the number of words is not know when the program is being written, a loop is needed to process the words.
- The loop needs to do two things for each word: compute its length and print it out.
Some program runs
Enter text: All hoos got your back
Supplied text = All hoos got your back
words = ['All', 'hoos', 'got', 'your', 'back']
All : 3
hoos : 4
got : 3
your : 4
back : 4
Enter text: It was a dark and stormy night
Supplied text = It was a dark and stormy night
words = ['It', 'was', 'a', 'dark', 'and', 'stormy', 'night']
It : 2
was : 3
a : 1
dark : 4
and : 3
stormy : 6
night : 5
Enter text:
Supplied text =
words = []
Program on_average.py
Task
- 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.
Two program runs
Enter text: over the hill and through the dale
Average word length: 4.0
Enter text: row row row your boat
Average word length: 3.4
Program again_and_again.py
Task
- Prompt and get user-supplied text
- Prompt, get, and convert to integer, a user-supplied integer.
- Print requested text requested number of times.
Realizations
- Because the requested number of times is not know when the program is written, a loop is needed.
- The loop should perform the same actio each time through the loop, print the input text.
Two possible program runs
Enter string to be printed: once again
Enter number of times to be printed: 3
once again
once again
Enter string to be printed: one more time
Enter number of times to be printed: 2
one more time
one more time
Program m_and_ns.py
Task
- Individually print out the integers in a user-indicated upward integer interval.
Realizations
- Because the size of the interval is not known when the program is being written, a loop is needed.
- Need a loop that supplies the integers in the interval one-at-a-time.
- The only thing needed to be done with the current integer from the interval is to print it out.
Some program runs
Enter start and end: 2 6
2
3
4
5
6
Enter start and end: 4 7
4
5
6
7
Program n_and_ms.py
— prints a user-specified downward interval of numbers
- Individually print out the integers in a user-indicated downward integer interval.
Realizations
- Because the size of the interval is not known when the program is being written, a loop is needed.
- Need a loop that supplies the integers in the interval one-at-a-time.
- The only thing needed to be done with the current integer from the interval is to print it out.
- Some program runs
Enter start and end: 5 1
5
4
3
2
0
Enter start and end: 12 11
12
Program inty.py
Task
- Prompt user for a list of space-separated integers
- Split reply into a list of strings
- Produce a new list that is the corresponding list of integers
- Print list of strings
- Print list of integers
Realization
- Function
int()
can only be applied to one string at a time.
- List of integers will need to be accumulated by processing the numeric strings one at a time.
- For each numeric string it will need to be converted to an integer and added to the list of integers being built
- The list of integers needs to be initalized to an empty list.
I can smile, see
Schrödinger's cat
© 2020 Jim Cohoon | Resources from previous semesters are available. |