1
00:00:00,740 --> 00:00:02,270
Hi there and welcome back.

2
00:00:02,270 --> 00:00:03,330
In this coding exercise,

3
00:00:03,330 --> 00:00:06,330
we looked at loops and if statements

4
00:00:06,330 --> 00:00:08,710
and we've got two parts to the coding exercise.

5
00:00:08,710 --> 00:00:10,680
In one of them, we have to use an if statement

6
00:00:10,680 --> 00:00:12,540
inside a loop and in the other one,

7
00:00:12,540 --> 00:00:16,200
we have to use the if statement's elif clause.

8
00:00:16,200 --> 00:00:18,650
But let's take a look at how we could solve this.

9
00:00:19,870 --> 00:00:20,703
First things first,

10
00:00:20,703 --> 00:00:23,970
you have to read the coding exercise description, of course

11
00:00:24,890 --> 00:00:28,440
and here we've got, now the coding exercise has two steps.

12
00:00:28,440 --> 00:00:30,490
You have to modify the code in the first step

13
00:00:30,490 --> 00:00:33,710
so that the evens list contains only the even numbers

14
00:00:33,710 --> 00:00:34,890
of the numbers list.

15
00:00:34,890 --> 00:00:36,940
So we have the numbers list down here.

16
00:00:36,940 --> 00:00:39,160
It has numbers from one to nine

17
00:00:39,160 --> 00:00:42,150
and then we have an evens list that is empty

18
00:00:42,150 --> 00:00:45,750
and here we have a for loop for number in numbers

19
00:00:45,750 --> 00:00:48,920
and every number we're going to append,

20
00:00:48,920 --> 00:00:50,840
so clearly the evens list at the moment

21
00:00:50,840 --> 00:00:52,930
is going to end up with all the same numbers

22
00:00:52,930 --> 00:00:54,530
as the numbers list.

23
00:00:54,530 --> 00:00:55,430
So we want to make sure

24
00:00:55,430 --> 00:00:58,520
that only the even numbers get appended.

25
00:00:58,520 --> 00:01:00,610
And the description here says

26
00:01:00,610 --> 00:01:03,120
that you do not need to print anything.

27
00:01:03,120 --> 00:01:05,930
So we will just append things there and that's it.

28
00:01:05,930 --> 00:01:07,690
For part two, we need to add a clause

29
00:01:07,690 --> 00:01:09,890
to the if statement such that is the user's input

30
00:01:09,890 --> 00:01:11,520
is q in lowercase,

31
00:01:11,520 --> 00:01:14,820
then our programme prints Quit with the uppercase Q.

32
00:01:14,820 --> 00:01:16,840
And remember, that Python will care about uppercase

33
00:01:16,840 --> 00:01:18,990
and lowercase letters, so that's important.

34
00:01:20,640 --> 00:01:23,030
Let's bring this back up here

35
00:01:23,030 --> 00:01:24,720
and here we have the if statement.

36
00:01:24,720 --> 00:01:26,630
If the user's input is a in lowercase,

37
00:01:26,630 --> 00:01:27,520
we're printing Add,

38
00:01:27,520 --> 00:01:30,380
so we have to add another if clause there

39
00:01:30,380 --> 00:01:33,420
to make sure that if it's q, we print Quit.

40
00:01:33,420 --> 00:01:35,780
Let's tackle part one first.

41
00:01:35,780 --> 00:01:37,170
Here we're appending every number

42
00:01:37,170 --> 00:01:39,430
in the numbers list to the evens list

43
00:01:39,430 --> 00:01:40,540
so what we have to do is make sure

44
00:01:40,540 --> 00:01:42,130
that we only append some numbers,

45
00:01:42,130 --> 00:01:43,640
the numbers that are even.

46
00:01:43,640 --> 00:01:45,970
In order to check for even numbers in Python,

47
00:01:45,970 --> 00:01:48,110
we can use the modulus operator.

48
00:01:48,110 --> 00:01:51,975
We can say if number is divisible by two,

49
00:01:51,975 --> 00:01:52,820
so what the modulus operator does

50
00:01:52,820 --> 00:01:54,630
is it gives us the remainder

51
00:01:54,630 --> 00:01:57,690
of a division, so if the number divided by two

52
00:01:57,690 --> 00:02:00,860
has no remainder, then this will be zero

53
00:02:00,860 --> 00:02:03,190
and that is how we can tell whether a number is divisible

54
00:02:03,190 --> 00:02:05,140
by another number.

55
00:02:05,140 --> 00:02:06,550
So here we'll check whether number

56
00:02:06,550 --> 00:02:08,100
is divisible by two

57
00:02:08,100 --> 00:02:11,210
and if it is, then we will append the events list.

58
00:02:11,210 --> 00:02:12,740
If it's not, then we won't.

59
00:02:12,740 --> 00:02:14,940
Going to part two, all we have to do here

60
00:02:14,940 --> 00:02:16,693
is add an elif clause.

61
00:02:17,571 --> 00:02:21,630
So we can say elif user_input is equal to q,

62
00:02:21,630 --> 00:02:22,683
then we will print.

63
00:02:24,500 --> 00:02:27,120
You could just do an if statement, a separate one

64
00:02:27,120 --> 00:02:29,550
but that will be a little bit slower in Python terms,

65
00:02:29,550 --> 00:02:31,600
so if you can use the elif here,

66
00:02:31,600 --> 00:02:32,570
then that's better.

67
00:02:32,570 --> 00:02:34,270
You can use the elif because the user input

68
00:02:34,270 --> 00:02:37,320
will never be both a and q at the same time,

69
00:02:37,320 --> 00:02:39,900
so there's no sense in checking both of them.

70
00:02:39,900 --> 00:02:41,040
If the user's input is a,

71
00:02:41,040 --> 00:02:43,550
then we're not going to check whether it's q.

72
00:02:43,550 --> 00:02:45,880
So that will give us a little bit of a speed boost,

73
00:02:45,880 --> 00:02:47,850
a very small one granted.

74
00:02:47,850 --> 00:02:48,683
Once you've got that,

75
00:02:48,683 --> 00:02:50,200
you can check the solution

76
00:02:50,200 --> 00:02:51,550
and make sure it's correct.

77
00:02:53,110 --> 00:02:54,530
And here we can see that it is correct,

78
00:02:54,530 --> 00:02:56,903
so we can continue when we're happy with that.

79
00:02:57,920 --> 00:02:59,300
As always, if you have any questions,

80
00:02:59,300 --> 00:03:01,250
do post them in the course Q and A

81
00:03:01,250 --> 00:03:04,000
and we'll be more than happy to help you out.

82
00:03:04,000 --> 00:03:05,690
Thanks for joining me in this video.

83
00:03:05,690 --> 00:03:08,440
Hope it's been useful and I'll see you in the next one.

