What are dictionaries?
Dictionaries are data structures that store key:value pairs.
The key is the thing that you use to find an item in a dictionary.
The value is the item that is stored under that key.
When should I use them?
Dictionaries should be used where you need to store data in a data structure and access it by it’s name.
Dictionaries are un-ordered, so that order that you put an item in a dictionary is not necessarily the order that your item is stored in. Other other words if you need to maintain data order, use a list instead.
Here are a few live script examples to have a play with to see what Dictionaries could be useful for.
Challenge 29 – Takeaway App
Create a program with a dictionary containing the names and telephone numbers of 5 takeaways.
- Your program should ask the user to enter a takeaway name, and then print out the matching number.
- If the takeaway cannot be found the print out an appropriate message.
At the start of the program it should print out the names of all the takeaways, so the user know which options they have to choose from.
Challenge 30 – Car counter
Create a program that a user can use to keep track of what make of cars pass by on a busy road.
- The program should ask for 10 cars and should keep track of the number of cars of that make, using a dictionary.
- The program should be adapted so that cars will be keep being added until an empty string is entered, then it should print out the contents of the dictionary.
- The program should display the contents of the dictionary, sorted in frequency order (highest frequency first).
Challenge 31 – Telephone Contacts app
You should create an app that asks the user to enter some contact names and telephone numbers.
Your program should store each name and telephone number in a dictionary
- The program should ask the user to enter 3 names / numbers.
- The program should display all the names and numbers at the end of the program.
- The program should keep adding names / numbers until the user enters and empty string for a name, then it should stop.
- The program should give the user the option to search for a contact’s telephone number.
- Your program should be adapted to have 4 screens:
- A main menu
- An add screen ( to add new contacts
- A search screen (to search for contacts)
- A view screen(to display all contacts)
Challenge 32 – Game Stats adder.
You should create a program that allows the user to add stats for a player. (e.g. strength 20, damage 30 , speed 50, etc)
- Your program should add all the stats from one character and then print out a full stat sheet at the end.
- Your program should include a list of valid stat categories and ensure that only valid categories are added.
- Your program should check the amounts entered are valid integers between the range 0 to 100.
- Your program should be adapted so that stats for multiple players’s stats can be added.
- Your program should give the user to option to select which player’s stat sheet should be printed out.