Before session 11
1. Dictionaries
Before the next class session, watch the videos made by Chuck Severance from University of Michigan:
Slides in English and in Russian
Textbook in English
Textbook in Estonian (Andmestruktuurid: hulgad, sõnastikud, tabelid)
2. Test
Go to Moodle and take the test on video lectures under Session 11.
3. Examples
This week we are going to get an insight into dictionaries.
NB! Make sure that text files (.txt) are in the same folder as your Python code. Otherwise Python will not find these files.
Example 1. Meetings
The file meetings.txt contains records of meetings. Each number in the file indicates a day of the week when an appointment takes place (not the number of appointments); so 1 means Monday, 2 means Tuesday etc. The following program reads the data from the file, creates a dictionary and prints out its keys and the values. The keys contain the days of a week, and the values are the numbers of planned appointments on that day.
def day_of_week(n): days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] return days[n-1] ffile = open("meetings.txt") meetings = dict() for line in ffile: try: number = int(line) day = day_of_week(number) meetings[day] = meetings.get(day,0) + 1 except: print("Invalid value") print(meetings)
Example 2. Inventory
This program helps compile an inventory of all the fruits in the shop. Dictionary inventory contains the data about how many fruits there should be in the shop (fruit names and their amounts). The program has a loop which repeatedly asks the user for a fruit. If the fruit is in the dictionary, the program outputs the amount of that fruit in the shop, and asks the user if he wants to update the amount of the fruit. If the fruit is not in the dictionary, the programs asks the user about the amount of the fruit and adds a new entry into the dictionary. The program continues asking until the user enters “done”.
inventory = {'apple': 430, 'banana': 312, 'orange': 525, 'pear': 217} while True: fruit = raw_input('Enter a fruit (done to quit): ') if fruit == 'done': break if fruit in inventory: print 'Stock information for', fruit, 'is', inventory[fruit] upd = raw_input('Do you want to update it? (yes/no) ') if upd == 'yes': number = int(raw_input('How many ' + fruit + ' do you have? ')) inventory[fruit] = number else: print 'There is no information about', fruit, 'in the shop' number = int(raw_input('How many ' + fruit + ' do you have? ')) inventory[fruit] = number
4. Solve and submit
Exercise 1. Birthdays
The birthdays in the file are written in format yyyy-mm-dd (see an example). Write a program that reads the data from the file and creates a dictionary with the following structure. The keys of the dictionary are names of weekdays (i.e. Monday, Tuesday etc), and the value of a key is the numbers of people who have been born on that weekday. Also, the program should have a function weekday_name which takes the number of a weekday as its parameter and returns the name of the weekday (the function should not contain if-statements).
To find the weekday, use Python's standard module datetime:
import datetime date = datetime.datetime(2018, 11, 13) print(date.weekday())
Exercise 2. Dictionary
To learn a new language, a very convenient tool is a dictionary, where we can look up words and add new words. Write a program that repeatedly asks the user for a foreign word. If the word is in the dictionary, then the program prints out its translation. If there is no such word in the dictionary yet, the program should ask the user for its translations and add the word together with its translation to the dictionary. When the user is asked for a new word and the user enters done, the program outputs the total number of words in the phone book and all the records from the dictionary.
Here is an example of program output:
Enter a word (done to quit): stjärtand There is no information for stjärtand What does stjärtand mean? pintail Enter a word (done to quit): silkesapa There is no information for silkesapa What does silkesapa mean? marmoset Enter a word (done to quit): brushane There is no information for brushane What does brushane mean? ruff Enter a word (done to quit): silkesapa silkesapa means marmoset Enter a word (done to quit): brushane brushane means ruff Enter a name (done to quit): done The dictionary has 3 entries in it All records in the dictionary: {'stjärtand': 'pintail', 'silkesapa': 'marmoset', 'brushane': 'ruff'}
Optional. Modify your program so that it reads the dictionary from the file at the beginning of the session and writes the new dictionary to the same file at the end of the session, so that words from previous sessions are already in the dictionary when the new session starts.
Go to Moodle and upload your solutions under homework for Session 11 homework.