Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,280 --> 00:00:04,720
Now up to this point, you've seen us use if statements,
2
00:00:04,750 --> 00:00:06,460
else statements, elif statements,
3
00:00:06,610 --> 00:00:10,600
multiple if statements as well as nested if statements.
4
00:00:10,990 --> 00:00:15,990
But the thing that we haven't been able to do thus far is to check for multiple
5
00:00:16,780 --> 00:00:20,440
conditions in the same line of code.
6
00:00:21,250 --> 00:00:26,140
So how would we be able to combine different conditions and say, well,
7
00:00:26,200 --> 00:00:31,200
is the pizza large and the user wants pepperoni and extra cheese all in the
8
00:00:32,290 --> 00:00:34,510
same line of code? Well,
9
00:00:34,570 --> 00:00:37,900
to do this we would need to learn about the logical operators.
10
00:00:38,290 --> 00:00:42,280
So there's three of them that are really useful: and, or and not.
11
00:00:43,180 --> 00:00:44,830
So let's take a look at the first one.
12
00:00:45,280 --> 00:00:49,060
When you combine two different conditions using an and operator,
13
00:00:49,390 --> 00:00:51,310
they both have to be true,
14
00:00:51,310 --> 00:00:55,390
both A and B for the entire line of code to be true.
15
00:00:56,320 --> 00:00:59,920
If just one of them is true, say A is true and B is false,
16
00:00:59,920 --> 00:01:04,780
or A is false and B is true, then the overall thing evaluates to false.
17
00:01:06,070 --> 00:01:08,800
So if we take a look just inside the console,
18
00:01:08,830 --> 00:01:13,450
and in fact, I'm going to clear the console, and I write some code here.
19
00:01:13,480 --> 00:01:14,770
Let's say that, um,
20
00:01:15,250 --> 00:01:20,250
A = 12 right? Now let's say that I wanted to check whether if is A greater
21
00:01:23,320 --> 00:01:27,460
than 15? Well that is going to be false.
22
00:01:28,060 --> 00:01:32,410
Now what about is A greater than 10? Well,
23
00:01:32,410 --> 00:01:33,340
that's going to be true.
24
00:01:33,940 --> 00:01:38,680
Now if I combined this using an and statement, I could say,
25
00:01:38,680 --> 00:01:43,680
well is A greater than 10 and A is less than 13 and hit enter, then I would get
26
00:01:49,450 --> 00:01:54,450
true because both A is greater than 10 and A is less than 13 are true.
27
00:01:55,990 --> 00:01:58,660
So in this case, when both conditions are true,
28
00:01:59,110 --> 00:02:02,320
then this entire line gets evaluated to true.
29
00:02:02,950 --> 00:02:06,100
But see what happens when just one of them is false.
30
00:02:06,250 --> 00:02:11,250
So is A greater than 15 and A less than 13? A is greater than 15 is false,
31
00:02:13,960 --> 00:02:17,590
A is less than 13 is true and now we get false.
32
00:02:18,580 --> 00:02:23,470
So that's what happens when you combine different conditions using and. Now if
33
00:02:23,470 --> 00:02:26,650
you only needed one of the conditions to be true,
34
00:02:27,040 --> 00:02:29,980
then you could use the or operator instead.
35
00:02:30,160 --> 00:02:35,160
So if a C or D were true or if they're both true,
36
00:02:35,740 --> 00:02:37,240
then it'll evaluate to true.
37
00:02:38,110 --> 00:02:41,680
It's only when both C and D are false
38
00:02:42,130 --> 00:02:44,530
does this statement actually become false.
39
00:02:45,640 --> 00:02:50,640
Now the final one is the not operator and all that this does is it basically
40
00:02:51,160 --> 00:02:55,360
reverses a condition. So if the condition is false,
41
00:02:55,390 --> 00:02:58,900
then it becomes true. If it's true, then it becomes false.
42
00:02:59,830 --> 00:03:01,390
If I wrote not
43
00:03:01,570 --> 00:03:06,570
and then I wrote the condition A is greater than 15. A greater than 15 is
44
00:03:09,550 --> 00:03:14,410
definitely false, 12 is less than 15. But by putting the not there
45
00:03:14,410 --> 00:03:18,340
it's going to reverse this, so going from false to true.
46
00:03:18,580 --> 00:03:21,790
So when I hit enter, you'll see it is now true.
47
00:03:23,530 --> 00:03:25,480
Coming back to our rollercoaster ticketing,
48
00:03:25,720 --> 00:03:30,670
let's say that the rollercoaster company decided that for everybody who was
49
00:03:30,670 --> 00:03:34,600
having a midlife crisis, they would give them free tickets.
50
00:03:35,020 --> 00:03:36,790
And according to Wikipedia,
51
00:03:36,880 --> 00:03:41,880
midlife crises typically occur when you're a 45 to 55 years old.
52
00:03:42,760 --> 00:03:46,390
Let's see if we can incorporate this into our code.
53
00:03:46,960 --> 00:03:51,490
So let's say that in addition to these existing price categories,
54
00:03:51,700 --> 00:03:56,700
what if you had to add a separate price category for those people who are aged
55
00:03:57,190 --> 00:04:02,190
between 45 and 55 and those people get to ride for free?
56
00:04:03,700 --> 00:04:06,910
Do you think you would be able to change the code using what you've learned
57
00:04:06,940 --> 00:04:11,940
about logical operators in order to incorporate this addition to our program?
58
00:04:12,730 --> 00:04:14,260
Pause the video and give that a go.
59
00:04:17,970 --> 00:04:21,720
So we've currently got three conditions: age,
60
00:04:21,750 --> 00:04:26,750
less than 12, age between 12 and 18, and finally everybody else.
61
00:04:27,750 --> 00:04:31,770
Now instead of just finishing up there,
62
00:04:32,580 --> 00:04:34,770
let's go ahead and add another elif.
63
00:04:35,130 --> 00:04:37,740
And here we're going to combine two conditions.
64
00:04:38,250 --> 00:04:43,250
We're going to say if the age is greater than or equal to 45 and the age is less
65
00:04:47,790 --> 00:04:52,790
than or equal to 55, then we get to catch that midlife crisis window.
66
00:04:54,840 --> 00:04:55,080
Well,
67
00:04:55,080 --> 00:05:00,030
in this case, we're going to print something like-- we're going to say "Everything's
68
00:05:00,030 --> 00:05:02,520
going to be ok. Have a free ride on us!"
69
00:05:04,530 --> 00:05:05,190
And in fact,
70
00:05:05,190 --> 00:05:10,190
we don't need to modify the bill in any way because we know that with
71
00:05:11,820 --> 00:05:16,820
if, elif and else statements is that once this condition matches,
72
00:05:19,260 --> 00:05:22,350
then everything that's inside this block,
73
00:05:22,350 --> 00:05:26,850
so everything that's indented inside this elif is going to be carried out
74
00:05:26,910 --> 00:05:28,350
namely printing this out,
75
00:05:28,890 --> 00:05:33,890
and then it's going to skip the rest of this if/else block and continue on.
76
00:05:36,630 --> 00:05:37,890
Now, if they want to photo this,
77
00:05:37,890 --> 00:05:41,790
they'll have to pay $3 but at least their ticket is free.
78
00:05:42,150 --> 00:05:47,150
And we've been able to do this because we know about the and logical operator.
79
00:05:48,720 --> 00:05:49,920
In the next lesson,
80
00:05:49,980 --> 00:05:54,960
we're going to practice using logical operators and more by completing another
81
00:05:54,960 --> 00:05:58,380
code challenge. So for all of that and more, I'll see you there.
7792
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.