1
00:00:00,000 --> 00:00:03,990
Some languages have a special control structure, called switch, or case, or

2
00:00:04,000 --> 00:00:08,990
something like that, that allows you to select from multiple choices of a single variable.

3
00:00:09,000 --> 00:00:10,990
Python doesn't have one.

4
00:00:11,000 --> 00:00:15,990
So, we're going to take a look at one particular strategy where you can do this

5
00:00:16,000 --> 00:00:21,990
very easily within Python, and you'll see that it's not necessarily a weakness in the language;

6
00:00:22,000 --> 00:00:23,990
it's just different way of looking at things.

7
00:00:24,000 --> 00:00:32,990
So, we'll go ahead and make a working copy of switch.py, call it switch-working.

8
00:00:33,000 --> 00:00:39,990
We'll open our working copy, and we'll start by declaring a dictionary. We'll call it

9
00:00:40,000 --> 00:00:56,990
choices, and we'll just put in a few choices here, one first, two second, three,

10
00:00:57,000 --> 00:01:07,990
and we'll declare a variable and then we'll select based on that variable.

11
00:01:08,000 --> 00:01:16,990
So, when I save this and run it, you'll see that we have selected one and we

12
00:01:17,000 --> 00:01:23,990
printed out first. And if I change this to say three, save that and run it, it'll save third.

13
00:01:24,000 --> 00:01:28,990
So, here we've been able to very easily select based on a number of choices, and

14
00:01:29,000 --> 00:01:32,990
we could make this a very long list if we wanted to, and we can add things,

15
00:01:33,000 --> 00:01:35,990
delete things from the list, and change things in the list, and this will all

16
00:01:36,000 --> 00:01:37,990
work exactly as expected.

17
00:01:38,000 --> 00:01:41,990
The one thing this does not do well is if I select something that's not in the

18
00:01:42,000 --> 00:01:47,990
list, you'll see that we get an error, save that and run it.

19
00:01:48,000 --> 00:01:52,990
Of course, we can trap this error with try and accept, or there is an even

20
00:01:53,000 --> 00:01:57,990
easier way to do this. We can use the get method of the dictionary object,

21
00:01:58,000 --> 00:02:05,990
so you get V comma, and then I can put in a default result, and I can say other.

22
00:02:06,000 --> 00:02:10,990
And so what that does is it looks for V in the dictionary, and if it doesn't find

23
00:02:11,000 --> 00:02:11,990
it, it gives you back this value.

24
00:02:12,000 --> 00:02:16,990
So, if I save that and run it, you can see we get other, because we have seven.

25
00:02:17,000 --> 00:02:23,990
If I put in five, then I'll get fifth. Save and run and there is fifth.

26
00:02:24,000 --> 00:02:28,990
So, this is a very simple way to do what most people use switch statements for

27
00:02:29,000 --> 00:02:33,990
and you'll see some other examples, including an example of executing code based

28
00:02:34,000 --> 00:02:44,000
on multiple choices, later on in the course.

