1
00:00:05,200 --> 00:00:09,922
At the moment, our program uses the country 
name as the key into the countries dictionary. 

2
00:00:10,000 --> 00:00:13,195
We also want to allow the user to 
enter a two-letter country code. 

3
00:00:13,440 --> 00:00:17,227
We have all the data we need, but we 
have to decide how we're going to use it. 

4
00:00:17,360 --> 00:00:19,829
One approach would be to create 
a separate dictionary, 

5
00:00:19,829 --> 00:00:24,514
to look up the country names for the codes.
So we might do something like this: 

6
00:00:35,520 --> 00:00:40,480
We'd have to define an empty dictionary called 
code_lookup, at the start of the program. 

7
00:00:40,480 --> 00:00:43,120
Then we could check the length of 
the user's input, and look up the 

8
00:00:43,120 --> 00:00:49,120
country name if they only enter two letters.
That's a viable solution, and would work fine. 

9
00:00:49,120 --> 00:00:52,646
If you want to try to do it that 
way, it would be a good exercise. 

10
00:00:52,720 --> 00:00:57,173
I'm going to use a different approach, because 
it might not be obvious that you can do this. 

11
00:00:57,280 --> 00:01:02,720
There's no reason why values, in a dictionary, 
can't have more than one key. We can add another 

12
00:01:02,720 --> 00:01:07,200
key to the countries dictionary, and give 
it the same value as the country name. 

13
00:01:07,200 --> 00:01:11,216
I'll comment out that line I 
just typed, and add a new key: 

14
00:01:23,360 --> 00:01:27,988
The countries dictionary will now contain two 
references to the dictionaries for each country. 

15
00:01:28,080 --> 00:01:32,468
One with the country name as the key, and 
another with the country code as the key. 

16
00:01:32,640 --> 00:01:36,181
Note that the individual country 
dictionaries are not duplicated. 

17
00:01:36,240 --> 00:01:41,597
As we saw in the last section, countries is 
storing references to the individual dictionaries. 

18
00:01:41,680 --> 00:01:47,654
They're not being copied by the code on line 22.
The dictionary will use more memory, of course. 

19
00:01:48,000 --> 00:01:51,072
But not significantly more than 
creating another dictionary. 

20
00:01:51,280 --> 00:01:55,273
This approach has the advantage that we don't 
have to change the remainder of our code. 

21
00:01:55,273 --> 00:02:00,587
You might want to change the prompt, on 
line 27, but that's just a cosmetic change. 

22
00:02:00,800 --> 00:02:04,110
This code will work with either 
a country name, or a code. 

23
00:02:04,160 --> 00:02:09,520
I'll run the program, to confirm that. Quit first, 
if you've still got the program running from the 

24
00:02:09,520 --> 00:02:15,040
previous video, then run the program again.
I'll enter au, which is the ISO country code 

25
00:02:15,040 --> 00:02:19,653
for Australia, and the program 
prints the correct capital city. 

26
00:02:21,760 --> 00:02:26,181
Entering Australia also works, 
and we get "Canberra" again. 

27
00:02:29,760 --> 00:02:33,440
Alright, that's been a useful example 
of reading data from a text file, 

28
00:02:33,440 --> 00:02:36,436
and getting it into a format 
that we can use with our code. 

29
00:02:36,560 --> 00:02:41,760
Before we look at writing data to a text 
file, practise with some changes to this code. 

30
00:02:41,760 --> 00:02:45,440
It would be more informative if you printed 
a suitable message, for countries like 

31
00:02:45,440 --> 00:02:50,320
Antarctica that don't have a capital city.
To test that change, you'll want to identify 

32
00:02:50,320 --> 00:02:54,880
the countries that don't have a value for their 
capital field. Write some code to iterate over 

33
00:02:54,880 --> 00:02:59,040
the dictionary, and print the country 
name of any country without a capital. 

34
00:02:59,040 --> 00:03:03,840
Expand that code, to print the country 
and field name for all blank values. 

35
00:03:05,920 --> 00:03:07,520
You could also print a suitable message 

36
00:03:07,520 --> 00:03:10,566
if the user enters something 
invalid for the country name. 

37
00:03:10,640 --> 00:03:14,812
Those are some ideas for you to use this 
example, to practise your programming. 

38
00:03:14,960 --> 00:03:18,641
Experiment, have fun, and I'll 
see you in the next video.

