Class 22 — Wednesday March 14
Test examples and "while" examples
Look both ways
Agenda
- Continue exploration
while
statement and ofif
statement
- Test practice
To do
- Examine artifacts on collab.
- Examine
while
epistle
- Prepare for after the test by looking at
dict
module documentation
More 'while' Examples and Test Review
- Program keep_quizzing.py
- Program first_five_proper.py
- Program guess_computer_number.py
- Program binary_checkerboard.py
- Program leap_year.py
Problem keep_quizzing.py
- Program an automated grading system that allows the student user to keep taking quizzes until they reach a desired average.
- First, the user is prompted to enter their desired score, out of 100.
- Next, the user enters their first quiz score, out of 100.
- While their running average quiz score is less than their desired grade, the user will be prompted to enter more quiz scores.
- Once their running average is greater than or equal to their desired score, the program congratulates the user, and prints out their final total.
-
Some program runs
Enter desired score: 92
Enter quiz score: 87
Enter quiz score: 88
Enter quiz score: 97
Enter quiz score: 99
Congratulations! Your average score is 92.75
Enter desired score: 88
Enter quiz score: 90
Congratulations! Your average score is 90.0
Problem first_five_proper.py
- The computer takes names from the user one at a time.
- Once the user has typed 5 properly capitalized names (first letter capitalized, rest of name lower case), the computer prints out a list of just those 5 names. Note that the user need not type 5 "correct" names in a row.
Enter name: Elizabeth
Enter name: Rachel
Enter name: Luther
Enter name: ray
Enter name: Nate
Enter name: Upsorn
[Elizabeth, Rachel, Luther, Nate, Upsorn]
Enter name: Aphrodite
Enter name: Hera
Enter name: Demeter
Enter name: Diana
Enter name: Cupid
[Aphrodite, Hera, Demeter, Diana, Cupid]
Test prep: leap_year.py
- The computer takes in a year from the user.
- If the year is divisible by 4, the programs prints out "It's a leap year"
- If the year is divisible by 2, but NOT divisible by 4, the programs prints out "It's a Winter Olympics year."
- If the year is not divisible by 2, the programs prints out "It's a regular year."
Test prep: binary_checkerboard.py
- This program prints out a table (like digitbox) that is a checkboard pattern.
- The 0th row and 0th column entry is 0. From there, alternate 1's and 0's.
- Hint: Use the sum of the row and column index to determine whether to enter a 1 or 0 for a specific cell.
Test prep: guess_computer_number.py
- The program generates a random number and prompts the user to guess. After every guess, the program replies "higher", "lower", or "equal to".
Agenda
- Introduce
dict
To do
- Examine artifacts
- Examine
dict
module documentation
- Examine
collections
epistle
- Examine
while
epistle
Examples
- Program romantic.py
- Program unromantic.py
- Program floradation.py
- Program even_odd.py
- Program good_doggie.py
Some data sets
Problem romantic.py
- Get a roman numeral and determine its integer equivalent
Some program runs
Enter Roman numeral: V
5
Enter Roman numeral: d
500
Enter Roman numeral: 0
Unknown
Problem unromantic.py
- Get a word and print its English equivalent
Some program runs
RecursionError: maximum recursion depth exceeded during compilation
Discussion
- Repeat the word carrot ten times out loud
- The human brain is great with associations; e.g., many of us associate apple with red, sky with blue, and programming with fun.
- A
dict
is Python's software equivalent of a collection of associations. In particular, adict
is an un-ordered collection of associations.
- The elements of a
dict
are called mappings. A mapping associates one object (called the key) with another object (called the value).
- The literal for an empty
dict
is{}
.
- A literal mapping is written using the
:
to separate the key with its value. For example, the below variableflora_to_color
is adict
that associates flora with colors.
flora_to_color = { 'spruce': 'green', 'apple': 'red', 'shamrock': 'green',
'banana': 'yellow', 'potato': 'brown', 'eggplant': 'purple' }
- The
len()
function works on adict
. For example,len(
flora_to_color
)
is 6, because the definition offlora_to_color
specified six mappings.
- The brackets
[]
operator in conjunction with assignment can be used to put a mapping into adict
. For example, the statement below adds the mapping'lemon'
to'yellow'
.
flora_to_color[ 'lemon' ] = 'yellow'
- The keys of a
dict
are always distinct. So, the following statement
flora_to_color[ 'potato' ] = 'golden'
would cause
flora_to_color
to lose its association of mappingpotato
tobrown
— its association with'potato'
is now only togolden
.
- As our
flora_to_color
example shows, the values of adict
are not necessarily unique.
- Python
dict
's have functionskeys()
andvalues()
for getting a hold of keys and values.
the_keys = flora_to_color.keys()
the_values = flora_to_color.values()
While there are never any duplicates in the keys, depending upon the dictionary there may be duplicates in the values.
- Other information about
dict
objects are listed below. In the below, assume thatd
is adict
.
d.get(k)
returns the value that keyk
maps to ind
. If there is no such value, returnsNone
.
d[k]
returns the value that keyk
maps to ind
. If there is no such value, an exception (error) occurs.
(k in d)
indicates whetherk
is a key value ind
.
(k not in d)
indicates whetherk
is not a key value ind
.
d.clear()
removes all mappings ind
.
del d[k]
removesk
’s mapping ind
.
- Information on a
dict
is available in the module documentation section of the website.
- Additional information on collections is in general is available in the collections epistile.
Problem even_odd.py
- Introduce the Python
dict
data structure
Some program runs
Enter a number: 3
3 odd
Enter even or odd: even
a number is even if its remainder divided by 2 is 0
Enter a number: 6
6 even
Enter even or odd: odd
a number is odd if its remainder divided by 2 is 1
Problem good_doggie.py
- For a series of dog information requests, the program reports what it knows about the dog.
- Its dog information data source is located at URL
- Brainstorming
- How should we store the dog information?
- How do we get and store the input?
- How many times do we set up the dog information
- What times of loops do we need?
- Sequential
- Nested
- Mixture of nested and sequential
Where am I
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |