1
00:00:05,200 --> 00:00:08,320
Ok, how did you get on?
Working with nested data 

2
00:00:08,320 --> 00:00:12,851
structures can be tricky at first, which 
is why we've given you a lot of practice with them. 

3
00:00:12,851 --> 00:00:17,280
They're very common when programming.
When you're working with nested data structures, 

4
00:00:17,280 --> 00:00:21,360
it's worth experimenting a bit, 
before trying to write the real code. 

5
00:00:21,360 --> 00:00:25,298
For example, you could start by 
getting the data for a single country. 

6
00:00:25,360 --> 00:00:29,428
That will let you see what you're working with.
I'll start by doing that: 

7
00:01:03,760 --> 00:01:09,170
I made sure our dictionary keys were in lowercase, 
when adding the keys to the countries dictionary. 

8
00:01:09,170 --> 00:01:13,280
You can see that on line 20.
To make sure that the user's input matches, 

9
00:01:13,280 --> 00:01:19,022
we convert their input to lowercase, by 
calling the casefold method, on line 24. 

10
00:01:19,022 --> 00:01:23,040
We have no control over what the user might type, 
so I've checked that their chosen country does 

11
00:01:23,040 --> 00:01:27,920
exist in our dictionary, on line 25.
Line 26 retrieves the value for the 

12
00:01:27,920 --> 00:01:32,560
chosen country, then we print it out.
Let's see what we're working with. 

13
00:01:32,560 --> 00:01:37,224
I'll comment out lines 19 and 22, because 
the output's getting a bit cluttered, 

14
00:01:38,560 --> 00:01:40,374
then run the program. 

15
00:01:41,760 --> 00:01:45,577
The program's waiting for 
input. I'll enter Australia. 

16
00:01:47,280 --> 00:01:52,675
The value that we get from the dictionary, 
on line 26, is another dictionary. 

17
00:01:53,760 --> 00:01:56,880
If you're unsure about what 
you're working with, print it out. 

18
00:01:56,880 --> 00:02:00,979
You don't have to write all the code in one go.
Write part of your code, 

19
00:02:00,979 --> 00:02:05,200
then test it and make sure it's doing what 
you expect, before you write the next bit. 

20
00:02:05,200 --> 00:02:09,520
The program isn't doing what we want yet, 
but it has shown us what we're working with. 

21
00:02:09,520 --> 00:02:13,711
Now that we know what we get from the 
countries dictionary, the next step is easy. 

22
00:02:13,840 --> 00:02:18,000
We can get the capital of the 
country, by using the key "capital". 

23
00:02:19,440 --> 00:02:23,417
Back in IntelliJ, retrieve the 
value for the key "capital": 

24
00:02:31,360 --> 00:02:36,284
Ok, we can add some bells and whistles to this 
code, but in terms of solving the challenge, 

25
00:02:36,284 --> 00:02:39,927
this code should work.
I'll run the program to check. 

26
00:02:41,600 --> 00:02:43,960
I'll start with Australia again ... 

27
00:02:45,280 --> 00:02:50,362
and we get Canberra. That's correct, 
Canberra is the capital of Australia. 

28
00:02:51,440 --> 00:02:57,000
You may have wrapped lines 24 to 27 in a loop, 
to allow the user to keep entering countries. 

29
00:02:57,200 --> 00:03:01,008
That's going to make it easier to test 
different countries, so I'll do that: 

30
00:03:04,160 --> 00:03:06,640
We can also make the output 
a bit more user-friendly, 

31
00:03:06,640 --> 00:03:09,467
and provide some way to break out of the loop:

32
00:03:41,330 --> 00:03:43,867
Alright, run the program again.

33
00:03:45,120 --> 00:03:48,203
This time, I'll enter Bermuda. 

34
00:03:49,440 --> 00:03:54,119
The capital of Bermuda is Hamilton.
Test the program with a few more countries, 

35
00:03:54,119 --> 00:03:58,316
and also enter invalid country 
names. The program shouldn't crash, 

36
00:03:58,316 --> 00:04:02,240
but you may want to print a message if the user 
enters something that isn't in our dictionary. 

37
00:04:02,240 --> 00:04:06,320
I won't do that on video, it's a good 
exercise for you to try on your own. 

38
00:04:06,320 --> 00:04:09,920
That's the challenge completed.
I'll finish this video by addressing a 

39
00:04:09,920 --> 00:04:13,600
question that a few students have asked.
When should you convert the user's 

40
00:04:13,600 --> 00:04:18,805
input to upper or lower case?
Often, you'll want to convert it as soon as you get it – 

41
00:04:18,805 --> 00:04:23,520
as I've done here, on line 25.
But that's not always ideal. 

42
00:04:23,520 --> 00:04:28,160
Have a look at the output from our program. 
"bermuda" starts with a lowercase "b", 

43
00:04:28,160 --> 00:04:32,400
even though I typed it with a capital "B".
If you need to use the original value that 

44
00:04:32,400 --> 00:04:36,807
was entered, somewhere in your code, 
then you have to store it unchanged. 

45
00:04:36,880 --> 00:04:41,120
Here, it might be more appropriate to convert 
to lowercase when we access the dictionary, 

46
00:04:41,120 --> 00:04:46,240
rather than as soon as we get the input.
That's not quite so easy to do, in this case, 

47
00:04:46,240 --> 00:04:52,640
because we use the lowercase string in two places. 
On line 26, we check if the key exists, 

48
00:04:52,640 --> 00:04:58,320
and on line 27 we use it to get the value from countries.
In this case, one solution is to convert 

49
00:04:58,320 --> 00:05:02,450
to lowercase, and store the 
lowercase key in another variable: 

50
00:05:09,886 --> 00:05:14,616
Now we can use this new 
variable, on lines 27 and 28: 

51
00:05:21,040 --> 00:05:24,597
Enter quit to exit the
program, then run it again. 

52
00:05:30,080 --> 00:05:32,106
I'll enter Bermuda again, 

53
00:05:34,320 --> 00:05:39,600
and we get the capital printed out. The 
output now has the original case of Bermuda. 

54
00:05:39,600 --> 00:05:45,280
Ok, that was a brief digression, to address 
possible problems when you transform data. 

55
00:05:45,280 --> 00:05:50,418
If you'll need the original data in your code, then 
it's your responsibility to make sure you keep it. 

56
00:05:50,480 --> 00:05:53,687
The other goal we had, was allow 
a country code to be entered, 

57
00:05:53,687 --> 00:05:58,500
instead of a country name. We'll see 
how to do that, in the next video.

