21.3. π©βπ» Code one section at a timeΒΆ
As a reminder, this is our prompt:
Build a program that replicates a physical dictionary that you might use. The program should that take five different input from the user. Each input will have two words and we will build a dictionary where the words are the keys and values.
Weβll start to build up the sections one at a time now! First, we need to pick a name for the dictionary. Weβll try to pick a clear name for each of these variables
We picked the variable name user_dictionary
because it will be a dictionary that is created by a user. Other names could be
appropriate as well! Though it may seem unnecessary, weβll add a print statement to remind ourself that user_dictionary
is empty.
Next weβll build up the for loop!
If we want to make sure that the for loop is iterating five times then we can add these print statements to execute so that we can track the progress of the program.
Next, weβlll get the input from the user!
Now weβll want to print out the response. Weβre expecting that it should be as string, so we should be able to add it to the print statement with other strings without any issue. If there is an issue, then something could be going wrong with how we are getting input from the user.
Now, we can separate the words so that we have our key and value to add to the dictionary!
Here we know that response
is a string that contains two words. We can use the split method to separate the words, which will give us
a list. The first word will be the key and the second word will be the value, so we can use indexing to access that information.
Finally, we add code to add the key and value pair into a dictionary. We can print out the final result of the dictionary once the for loop is over so that we can determine if it has been done correctly.