Before session 2
Dictionaries
Before the next class session, watch the videos made by Chuck Severance from University of Michigan:
Slides
Text-book: chapter 9 - dictionaries
Test
Go to Moodle and take the second test on the video lectures.
Homework
The deadline for homework is Sunday. Nevertheless, you can start with homework already before the session.
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 the file.
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). 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.
def day_of_week(n): days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] return days[n-1] try: ffile = open("meetings.txt") except: print("Can't open the file") else: 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
The present program helps compile an inventory of all fruits in the shop. Dictionary inventory contains the data about how many fruits there should be in the shop (the fruit names and their amount). 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 the 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 program 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 = input('Enter a fruit (done to quit): ') if fruit == 'done': break if fruit in inventory: print('Stock information for', fruit, 'is', inventory[fruit]) upd = input('Do you want to update it? (yes/no) ') if upd == 'yes': number = int(input('How many ' + fruit + ' do you have? ')) inventory[fruit] = number else: print('There is no information about', fruit, 'in the shop') number = int(input('How many ' + fruit + ' do you have? ')) inventory[fruit] = number
Exercise 1. Birthdays
The birthdays in the file are written in the format dd.mm.yyyy (see an example). Write a program that reads the data from the file and creates a dictionary. The keys of the dictionary are the names of the months, and the values of the dictionary are the number of birthdays in each month. Print the dictionary out. Also, the program should have a function month_name that takes the number of a month as its argument and returns the name of the month (the function should not contain if-statements).
Exercise 2. Phone book
When a person buys a new phone, there is only one phone number in the phone book (Emergency number - 112). Write a program that helps the person add phone numbers into the phone book. The program should repeatedly ask the user for a new contact name. If there is no such name in the phone book yet, the program should ask the user for the phone number. When the user is asked for a new contact name and the user enters done, the program outputs the total number of contacts in the phone book and all the records from the phone book.
Here is an example of the program output:
Enter a name (done to quit): Emergency number Emergency number's phone number is 112 Enter a name (done to quit): Andrew Parson There is no information for Andrew Parson What is Andrew Parson's number? 8806336 Enter a name (done to quit): Emily Everett There is no information for Emily Everett What is Emily Everett's number? 6784346 Enter a name (done to quit): Peter Power There is no information for Peter Power What is Peter Power's number? 7658344 Enter a name (done to quit): Emily Everett Emily Everett's phone number is 6784346 Enter a name (done to quit): done The phone book has 4 entries in it All records in the phone book: {'Emergency number': 112, 'Andrew Parson': '8806336', 'Emily Everett': '6784346', 'Peter Power': '7658344'}