During session 2
Exercises
NB! The practical session exercises are provided in such capacity that more experienced students would also find them interesting. It is not expected of you to get all of them done during one session. However, you are always welcome to complete the unfinished tasks at home!
1. A piece of code for a fantasy video game
In this task, write a piece of code for a fantasy video game. The code will help keep a record on the characters and their inventory. First, create a dictionary. The items of the inventory must be kept as keys of the dictionary (data type - string). The values of the dictionary are integers; they show how many of each item a player has. For example, a dictionary {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12} means a player has 1 rope, 6 torches, 42 gold coins, and so on.
Next, write a function display_inventory that takes a dictionary and displays it as follows:
Inventory: 1 rope 42 gold coin 6 torch 1 dagger 12 arrow Total number of items: 62
Hint: You can use a for loop to loop through all the keys in the dictionary.
2. Fantasy game inventory v.2
Imagine that vanquished dragon’s loot is represented as a list of strings like this:
dragon_loot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
Write a function called add_to_inventory(inventory, added_items) where the inventory is a parameter for a dictionary representing one’s inventory (like in the previous exercise) and the added_items is a parameter for a list (like dragon_loot). The add_to_inventory function should return a dictionary that represents the updated inventory. Note that the values in the added_items list might not be unique. Display the new inventory using the function from the previous exercise.
Example of the output (the first two programs together):
Inventory: 1 rope 42 gold coin 6 torch 1 dagger 12 arrow Total number of items: 62 Dragon's loot: Inventory: 45 gold coin 2 dagger 6 torch 1 rope 12 arrow 1 ruby Total number of items: 67
3. International vehicles
An international licence plate country code is 1-3 letter abbreviation in uppercase on the vehicle plate or sticker near the number plate on the rear of a vehicle. The code shows the country where a vehicle's registration plate was issued. For example, the code of Estonia is EST.
Create a .txt file (a database) for a border guard so that each line of the file contains a country code and the name of that country separated by a space.
Example of file countries.txt:
ER Eritrea FIN Finland F France H Hungary LT Lithuania EST Estonia S Sweden
Write a function dictionary_from_file which takes the name of the file as an argument and returns a dictionary based on the content of the file (the keys of the dictionary are country codes and the values are the country names).
Example of the function call (using the above-mentioned file):
>>> dictionary_from_file("countries.txt") {'EST': 'Estonia', 'F': 'France', 'H': 'Hungary', 'S': 'Sweden', 'LT': 'Lithuania', 'FIN': 'Finland', 'ER': 'Eritrea'}
Write a function codes_to_names with two arguments: one is for a list with country codes and the other one is for a dictionary country code - name pairs. The function has to return a list with the names of the respective countries. If any country code in the list is unknown, then the name of that country must be noted as Unknown in the returning list.
>>> d = dictionary_from_file("countries.txt") >>> codes = ['EST', 'CO', 'FIN', 'UY', 'BR', 'LT', 'ER'] >>> codes_to_names(codes,d) ['Estonia', 'Unknown', 'Finland', 'Unknown', 'Unknown', 'Lithuania', 'Eritrea']
Write a program that
- prompts the user for a file name;
- prompts the user for a string which contains country codes of vehicles crossing the border separated by spaces;
- uses the created functions and outputs the names of the countries where the vehicles were registered (one below the other).
Example of the program output:
Enter a file name: countries.txt Enter codes: EST CO FIN UY BR LT ER Estonia Unknown Finland Unknown Unknown Lithuania Eritrea
4*. Children and parents
The file children.txt contains parent's personal identification code, space, and child's personal identification code on each line. Another file names.txt contains person's personal identification code, space, colon, space and name of a person on each line. Assume that there are no duplicate names in the file and there is a name in file names.txt for each personal code from file children.txt.
Write a program that outputs one row per parent: parent's name, colon, space, and then the names of parent's children separated by commas and spaces. For example, using the given files, the following lines should be printed on the screen (the order of parents or their children is not important):
Madli Peedumets: Robert Peedumets, Maria Peedumets Peeter Peedumets: Robert Peedumets, Maria Peedumets Kadri Kalkun: Liisa-Maria Jaaniste Karl Peedumets: Peeter Peedumets
Hints:
- Open file names.txt and add the data into a dictionary (the keys of the dictionary are personal identification codes and the values are names).
- Open file children.txt and add the data into a new dictionary (the keys of the dictionary are parents' names and the values are lists with children names). NB! use the previously created dictionary for getting the names of parents via the codes.
- Loop through all the keys of the second dictionary, format and output the information about parents and children.
At the end of the session go to Moodle and submit all programs written during the session.