Class 21 – Wednesday October 16
You are so controlling
While I can go on – I do not repeat myself – Always a new way
Look both ways
Agenda
- Introduce
while
statement
- Introduce
dict
To do
- Examine artifacts
- Examine
dict
module
- Examine
collections
epistle
Downloads
- Program guess_what.py
- Program even_odd.py
- Problem romantic.py
- Problem unromantic.py
- Problem good_doggie.py
- Program que_dijiste.py
Maxims (among others)
- Read the problem in its entirety before problem solving
- Determine for yourself why the sample problem solutions are as they are
- Problem solve before you code
- Check whether your solution produces the same answers as the sample runs
- Check whether your solution matches problem specifications
To be considered
- How would you handle the problem of getting a yes or no reponse from a user, where case of the letters do not matter. Once gotten, the valid response is echoed (printed).
- Questions
- Do we know ahead of time, how many often we need to loop?
- Are there action(s) that need to be done multiple times?
- Are there action(s) that need to be done once?
- Some possible runs
Enter (yes / no): yeah
Enter (yes / no): yeap
Enter (yes / no): tá
Enter (yes / no): כן
Enter (yes / no): ndiyo
Enter (yes / no): haan
Enter (yes / no): YeS
yes
Enter (yes / no): nah
Enter (yes / no): không
Enter (yes / no): nope
Enter (yes / no): aʻole
Enter (yes / no): ei
Enter (yes / no): NO
no
Whiling away
- Like many programming languages, Python provides more than one way to iterate actions (repeatedly execute). We are concerned here with its
while
looping offering.
- The
while
loop differs from thefor
loop in that the number of times it iterates is not based on the size of a list or range; instead, iteration is based on a logical test expression.
- The basic
while
state has the form:
while ( test expression ) :
action
- When Python reaches a
while
loop, it will repeatedly execute the loop's actions as long as the test expression evaluates to true. When the test expression evaluates false, the program drops down to the statement following the loop. For example, consider yes_or_no.py
looking_for_a_yes_or_no = True
while ( looking_for_a_yes_or_no ) :
reply = input( 'Enter (yes / no): ' )
reply = reply.lower()
if ( reply in [ 'yes', 'no' ] ) :
looking_for_a_yes_or_no = False
print( reply )
- FYI: Python is one of the languages whose
while
statement also allows an optionalelse
. Theelse
action is executed once the test expression evaluates to false.
while ( test expression ) :
action
else :
action
Program guess_what.py
- Play a guessing name with the user. The program is to determine what number the user has guessedl, where the possible numbers are from 0 to 100,000.
- Questions
- How would you solve the problem if you were the guesser?
- Can we do this with a
for
loop? If, so what are some possible ways?
- Can we do this with a
while
loop? If, so what are some possible ways?
Program romantic.py
- Get a roman numeral and determine its integer equivalent
Three sample program runs
Enter Roman numeral: V
5
Enter Roman numeral: d
500
Enter Roman numeral: 0
Unknown
Program unromantic.py
- Get a word and print its English equivalent
Sample 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., we typically associate apple with red and January 1st with New Year's Day.
- A
dict
is Python's software equivallent of a collection of associations.
- In particular, a
dict
is unnordered collection of associations.
- The elements of
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 variableflora_to_color
defined below 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
.
len(
flora_to_color
)
is 6 because the definition offlora_to_color
included six mappings.
- The brackets
[]
operator in conjunction with assignment can be used to put a mapping into adict
.
- The statement below adds the mapping
'lemon'
to'yellow'
.
flora_to_color[ 'lemon' ] = 'yellow'
- The keys of a
dict
are always distinct. If there had already been a mapping with'lemon'
as the key, the above assignment would replace the association.
- As our
flora_to_color
example shows, the values of adict
are not necessarily unique.
- 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
.
del d[k]
removesk
’s mapping ind
.
(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
.
d.keys()
are the keys ford
. There are no duplicates among the keys.
d.values()
are the values of the keys ind
. There are duplicate values if more than one key maps to the same value.
- Information on a
dict
is always available in the module documentation section of the website.
- Additional information on collections is in general is available in the collections epistile.
Program even_odd.py
- Introduce the Python
dict
data structure
Two sample 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
Program 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
- Questions
- 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
Huh?
- dónde está mi lápiz
- donde esta mi lapiz
- hvor 是 该 tumili maleri av Munch
- est la baguette fraîche aujourd'hui
- est la baguette fraiche aujourd'hui
Program que_dijiste.py
- Reads and prints the babelfish csv-based translation dictionary
{'aab': 'water', 'aamadam': 'came', 'aamadand': 'came', 'aamad': 'came', 'aamadi': 'came',
...
'wollen': 'want', 'wort': 'word', 'wo': 'where', 'y': 'and', 'yo': 'I', 'Yo': 'I', 'yun': 'wool', 'yün': 'wool', 'zahl': 'number', 'zauberformel': 'spell', 'zauberhaft': 'enchanting', 'zehn': 'ten', 'zeichen': 'characters', 'zeigt': 'show', 'ziba': 'beautiful', 'zur': 'to', 'zu': 'to', 'zwei': 'two', '是': 'is', '该': 'the'}
Next class — what we really want to do
Enter phrase to be translated: dónde está mi lápiz
where is my pencil
Enter phrase to be translated: est la baguette fraîche aujourd'
is the baguatte fresh today
Enter phrase to be translated: je %#@#&# ból svayam!!
I >%#@#&#< hurt myself >!!<
Enter phrase to be translated: hvor er den skriget maleri af Munch
where is the scream painting by Münch
Enter phrase to be translated: hacer 뭔가 frais oggi
do something cool today
- Questions
- How should we store the translations?
- How should we store the input?
- How should we store the result?
- What times of loops do we need?
© 2019 Jim Cohoon | Resources from previous semesters are available. |