1
00:00:04,500 --> 00:00:08,180
We've seen how we can store values
in variables, and print them out.

2
00:00:08,180 --> 00:00:12,900
We've also looked at getting input from the keyboard,
in the previous section in this course.

3
00:00:12,900 --> 00:00:16,680
We'll now start getting Python to
make decisions, based on the values of

4
00:00:16,690 --> 00:00:21,640
variables and input. Just to quickly recap,
I'm going to just type in a quick

5
00:00:21,640 --> 00:00:25,640
program. I'm going to delete the last
three lines from the last video.

6
00:00:25,640 --> 00:00:34,200
I'm going to type name equals input parentheses and
double quotes, Please enter your name,

7
00:00:34,200 --> 00:00:41,320
colon and a space there, and on line 2,
age equals input parentheses double

8
00:00:41,320 --> 00:00:48,700
quotes again, How old are you comma space.
Then we want left and right curly braces,

9
00:00:48,700 --> 00:00:52,420
replacement fields, zero in the middle,
question mark to the right of the right

10
00:00:52,420 --> 00:00:59,500
curly brace and a space. Then a closing
double quote dot format name and two

11
00:00:59,500 --> 00:01:02,980
right parenthesis. Then line three, we're
going to print out the value of age,

12
00:01:02,980 --> 00:01:08,200
so print parentheses age. Alright, just as
a quick recap to what we've done

13
00:01:08,200 --> 00:01:11,290
previously, I'm gonna run that code now,
and remember to click into the run

14
00:01:11,290 --> 00:01:15,100
window as I did, before typing your name
if you're using an IDE like IntelliJ.

15
00:01:15,100 --> 00:01:19,100
So come down here and click on that and
I'll just move this down a little bit.

16
00:01:19,100 --> 00:01:25,080
I'm going to type in my name, enter, How
are you Tim, like to think I'm 21,

17
00:01:25,080 --> 00:01:29,980
press enter, 21 is shown in the
window. So note that we can use

18
00:01:29,980 --> 00:01:33,490
replacement fields in any string,
including the one we're using as a

19
00:01:33,490 --> 00:01:39,660
prompt to the input function. The input
function returns as str or string datatype.

20
00:01:39,660 --> 00:01:43,680
We have to convert it to an int
if we want age to represent a number,

21
00:01:43,680 --> 00:01:48,660
rather than the string, and we do that
with the int function. So if we change

22
00:01:48,660 --> 00:01:55,160
line 2 slightly. Instead of putting input parentheses
we'll put int, left parentheses.

23
00:01:55,160 --> 00:01:57,360
Then right at the end, now we've done that,
we need to go to the end

24
00:01:57,360 --> 00:02:02,300
of the line and add a right parenthesis
there, to keep Python happy.

25
00:02:02,300 --> 00:02:06,900
So using int and input together like this is very
common when you want to get a number

26
00:02:06,910 --> 00:02:11,080
from the user. It does have a serious
problem though. Your code will crash if

27
00:02:11,080 --> 00:02:15,069
you type anything that can't be
converted to an integer. Now we'll be

28
00:02:15,069 --> 00:02:17,420
looking at how to handle that later in the course.

29
00:02:17,420 --> 00:02:19,780
Until then, make sure your only enter valid

30
00:02:19,780 --> 00:02:26,580
numbers when our programs expect a number.
Alright, so if we just run this to confirm,

31
00:02:26,580 --> 00:02:33,099
enter your name again, Tim,
25, we're getting the value of 25.

32
00:02:33,099 --> 00:02:37,120
And let's just quickly show what would
happen if we do insist on typing

33
00:02:37,120 --> 00:02:42,420
something that's not a number. I'll type in
the text twenty.

34
00:02:42,420 --> 00:02:44,420
You can see we've got an error there,

35
00:02:44,420 --> 00:02:47,940
and again, we'll talk about how
to fix that later in the course.

36
00:02:47,940 --> 00:02:50,760
Right, at this point though, we've got a
number. We saw that that was working,

37
00:02:50,760 --> 00:02:54,960
and we can make, at this point, some decisions
based on the person's age.

38
00:02:54,960 --> 00:02:59,500
So let's start by checking if they're old enough to
vote. In many countries,

39
00:02:59,500 --> 00:03:05,320
including Australia, the legal age for voting is 18.
So let's come up and on line 5,

40
00:03:05,320 --> 00:03:13,880
type if space age is greater than, and the equal
sign, 18 colon press enter.

41
00:03:13,880 --> 00:03:18,840
So we started with the keyword if and followed it with
a condition, and here the condition is

42
00:03:18,840 --> 00:03:24,020
age greater than equal to 18. Now notice the
colon that I placed on the end of that line.

43
00:03:24,020 --> 00:03:26,980
That means we're about to start a
new code block, and in fact, we did

44
00:03:26,980 --> 00:03:31,000
start a new code block when I pressed
enter, and the IDE indented the next line

45
00:03:31,000 --> 00:03:35,720
for us automatically, as we've seen
previously. The indentation is very important.

46
00:03:35,720 --> 00:03:39,340
That's how Python knows that
we're starting a new block of code.

47
00:03:39,340 --> 00:03:42,340
Alright, so if they're old enough to
vote we'll print out a suitable message.

48
00:03:42,340 --> 00:03:47,480
So let's do that on line 6 and put
print parentheses double quotes,

49
00:03:47,480 --> 00:03:54,780
You're old enough to vote. Okay. So if the
condition is true,

50
00:03:54,780 --> 00:03:57,920
in other words,  the age entered is greater than or equal to 18,

51
00:03:57,920 --> 00:04:03,500
then we're printing out the message, You
are old enough to vote,

52
00:04:03,500 --> 00:04:08,100
and I'll just press backspace there to remove the
indentation. Let's run it.

53
00:04:08,100 --> 00:04:12,760
We'll do it a few times just to make sure, with
different ages, that it is working as expected.

54
00:04:12,760 --> 00:04:18,700
So if I start by typing my
name firstly, then the age is 18,

55
00:04:18,700 --> 00:04:26,110
You are old enough to vote, so that's
working. Tim and I'll try 17. We get just

56
00:04:26,110 --> 00:04:28,940
the value 17, we don't get a message
saying we're old enough to vote,

57
00:04:28,940 --> 00:04:36,760
and lastly, do one 50, Tim 50, You are old enough to vote.

58
00:04:36,760 --> 00:04:39,039
And again, remember to type a valid
number, or you'll get the error that I

59
00:04:39,039 --> 00:04:42,639
showed earlier in the video, if you're
not using a number. Alright, so what

60
00:04:42,639 --> 00:04:46,990
we've done there on line 5, that's how to
test a condition in Python, keyword

61
00:04:46,990 --> 00:04:50,620
if followed by condition, in this case,
age greater than equal to 18,

62
00:04:50,620 --> 00:04:54,800
and finishing the first, and finally ending
the line with a colon.

63
00:04:54,800 --> 00:04:58,779
Now conditions can be a lot more complex
than that, as we'll see. We know that a

64
00:04:58,780 --> 00:05:04,420
colon tells python to expect a new code
block, so line 6 is indented by 4 spaces.

65
00:05:04,420 --> 00:05:09,060
If age is less than 18, nothing happens,
which is a bit unfriendly. We can cater

66
00:05:09,069 --> 00:05:14,340
for that by adding an else clause to our
if keyword. Now we don't want any more

67
00:05:14,340 --> 00:05:19,700
code inside the if block, which means we
remove the indentation on the else line.

68
00:05:19,700 --> 00:05:22,800
So I've already done that. You saw me do
that earlier, but we'll start typing on

69
00:05:22,809 --> 00:05:29,529
line 7, else : press Enter.
Notice how IntelliJ has helpfully

70
00:05:29,529 --> 00:05:34,599
indented again. We'll type print
parentheses double quotes, Please come

71
00:05:34,599 --> 00:05:39,099
back in, and we use a replacement field
with a zero in between the left and

72
00:05:39,100 --> 00:05:48,340
right curly braces, space years, double quote comma,
and we'll put format parenthesis 18 minus age,

73
00:05:48,340 --> 00:05:53,460
and the two right closing parentheses. So
if the person's age is greater than or

74
00:05:53,469 --> 00:05:58,569
equal to 18, the first block of code is
executed, otherwise the second one is

75
00:05:58,569 --> 00:06:02,949
executed, asking them to come back when
they're 18, or more specifically, in a

76
00:06:02,949 --> 00:06:05,980
number of years, asking them to come back when they're 18.

77
00:06:05,980 --> 00:06:14,600
So we run this a few times just to make sure that
it works. Let's do that, Tim, 18,

78
00:06:14,600 --> 00:06:18,720
still get the same result, 18, You are old enough to vote.

79
00:06:18,720 --> 00:06:23,580
Tim, 50, You are old enough to vote. That's
working, and we'll do one more, this time

80
00:06:23,580 --> 00:06:30,740
if they're less than 18, Tim, we'll say 9, Please
come back in, and we made a typo there.

81
00:06:30,740 --> 00:06:34,839
I'll put a comma there. Notice I put a
comma there instead of a period, which is

82
00:06:34,840 --> 00:06:40,100
a subtle little bug. I'll do that.
We didn't see that until we entered in the value,

83
00:06:40,100 --> 00:06:43,860
so Tim, How old are you? 9. There it is
again,

84
00:06:43,870 --> 00:06:46,120
Please come back in nine years
and that's because I enter the age of

85
00:06:46,120 --> 00:06:52,780
9, and the calculation on line 18 was
18 minus what we entered, in this case, 9.

86
00:06:52,780 --> 00:06:56,680
So it says come back in 9 years, when of
course you'll be 18. Alright, so as we saw

87
00:06:56,680 --> 00:07:01,320
in the last video, a block can contain
several lines of code to execute.

88
00:07:01,320 --> 00:07:06,280
So let's go ahead and add something, a
second line under line 6, so on line 7

89
00:07:06,280 --> 00:07:12,920
I'm going to type print parenthesis
double quotes, Please put an X

90
00:07:12,920 --> 00:07:17,280
in the box,
and we'll close off the string and then

91
00:07:17,289 --> 00:07:21,880
right parenthesis. Because line 6 and
7 are in the same block, in other

92
00:07:21,880 --> 00:07:25,659
words, they're indented to the same level,
we should find that both messages will

93
00:07:25,659 --> 00:07:29,259
be printed if the age is greater than
equal to 18. So let's just run that to

94
00:07:29,260 --> 00:07:38,280
confirm that is the case. Please enter your name,
Tim, 19, You are old enough to vote.

95
00:07:38,280 --> 00:07:44,020
Please put an X in the box. There's often
several ways to do the same thing when programming.

96
00:07:44,020 --> 00:07:46,900
It may seem natural to deal with the voters first,

97
00:07:46,900 --> 00:07:50,360
but we can do that the other
way around as well. So to see what

98
00:07:50,360 --> 00:07:54,620
I mean I'm going to add some code.
I'll add that down here.

99
00:07:54,620 --> 00:08:03,720
We'll start on line 11. We'll leave line 10
blank. We're going to put if age is less than 18:

100
00:08:03,720 --> 00:08:08,940
and let's just copy the line, here,
we'll copy that line. Notice when you

101
00:08:08,940 --> 00:08:12,000
moved the cursor down it automatically
moved over to the indentation level. We're

102
00:08:12,040 --> 00:08:18,680
going to paste that as if the age is
less than 18, else:

103
00:08:18,680 --> 00:08:24,860
I'm going to grab the two lines up here.
I'm going to paste those in as well.

104
00:08:24,860 --> 00:08:29,200
Notice that IntelliJ helpfully indents
at the right line, or to the right

105
00:08:29,200 --> 00:08:32,659
indentation level, and again we're doing
things the other way around. We're not

106
00:08:32,659 --> 00:08:35,799
dealing with the voters first this time. We're
dealing with people who, in fact, aren't

107
00:08:35,799 --> 00:08:39,578
old enough to vote, first, and the people
who are, second, just to show you there's

108
00:08:39,578 --> 00:08:45,040
another way of doing the same thing. So
we run this. I'm going to enter the same

109
00:08:45,040 --> 00:08:51,880
thing twice now for Tim, 15, Please come
back in 3 years, as you can see.

110
00:08:51,880 --> 00:08:54,790
We've got exactly the same output two
times and that's because we've got two

111
00:08:54,790 --> 00:08:58,820
conditions in there, two code blocks if
you will. So if I enter that again,

112
00:08:58,820 --> 00:09:07,860
Tim, 21, and you can see we've got the
output again, duplicated,

113
00:09:07,860 --> 00:09:12,100
because effectively, we've duplicated, we've got
two if code blocks with the appropriate

114
00:09:12,100 --> 00:09:15,670
else clauses entered. So the code,
ultimately, that I've added does exactly

115
00:09:15,670 --> 00:09:19,260
the same thing, but it's checking the
condition the other way around.

116
00:09:19,260 --> 00:09:22,640
If the person is less than 18, we print the
message asking them to come back,

117
00:09:22,640 --> 00:09:26,660
otherwise, we print the two lines
asking them to tick the box.

118
00:09:26,660 --> 00:09:31,680
So it doesn't matter which way round you phrase the conditions?  Well in this example, no it doesn't,

119
00:09:31,680 --> 00:09:34,980
but in the next video, we'll see why it can matter.

120
00:09:34,980 --> 00:09:37,880
See you in the next video.

