1
00:00:04,710 --> 00:00:09,930
Alright, so let's talk more about conditional
operators in Python, but before we

2
00:00:09,930 --> 00:00:13,820
look at other conditional operators, let's
review the code from the last video.

3
00:00:13,820 --> 00:00:18,660
So on line 6, we test to see if the original
guess is less than 5. If it is, we print

4
00:00:18,660 --> 00:00:23,960
another message and allow another guess.
If the original guess is not less than 5,

5
00:00:23,960 --> 00:00:29,400
then line 13 checks if it's greater than
5 using elif. We allow another guess

6
00:00:29,400 --> 00:00:33,989
if it's greater than 5. If neither of those
conditions are true, then the guess must

7
00:00:33,989 --> 00:00:38,980
have been equal to 5. This is picked up
by the final else on line 20.

8
00:00:38,980 --> 00:00:42,640
There are better ways to write code like this, as we'll see shortly.

9
00:00:42,640 --> 00:00:48,540
We've duplicated exactly the same code in the if and elif
blocks. The duplicate lines are lines 8

10
00:00:48,540 --> 00:00:54,690
to 12 and also 15 to 19. Whenever you see
duplication like this happening, that's a

11
00:00:54,690 --> 00:00:56,900
good indication that it can be done
better.

12
00:00:56,900 --> 00:01:03,020
For now, though, this example has
illustrated a number of important concepts.

13
00:01:03,020 --> 00:01:07,700
Alright, so let's talk about if blocks. So a block
can be quite complex, including further

14
00:01:07,700 --> 00:01:11,990
if and else blocks, and much more
contained within it. When testing for

15
00:01:11,990 --> 00:01:16,520
equality, we can't use a single equal
symbol. The single equal is used for

16
00:01:16,520 --> 00:01:20,600
assigning values to variables, so when
testing for equality, we have to use two

17
00:01:20,600 --> 00:01:25,160
equal signs, the equals equals. An if
statement can include many elif parts,

18
00:01:25,160 --> 00:01:29,680
but there can only be one else. elif,
by the way, is short for else if.

19
00:01:29,680 --> 00:01:35,300
The else, if there is one, must come after all the
elif blocks. Duplicating code is

20
00:01:35,300 --> 00:01:40,000
generally a bad idea. There's almost
always a better way.

21
00:01:41,320 --> 00:01:45,360
When testing conditions, we can use any
of the value comparison operators.

22
00:01:45,360 --> 00:01:51,360
Now there are other types of comparisons we can perform but we'll focus on these 6 for now;

23
00:01:51,360 --> 00:01:53,640
less than, less than or equal to, greater than,

24
00:01:53,640 --> 00:01:58,120
greater than or equal to, equal to
and not equal to.

25
00:01:58,120 --> 00:02:03,600
Once again, remember that we test for equality using two equal symbols. That's something you'll

26
00:02:03,610 --> 00:02:07,810
forget at first, but you'll get a syntax
error if you use a single equal, which

27
00:02:07,810 --> 00:02:12,000
should remind you.
Alright, so now we know how to test for

28
00:02:12,000 --> 00:02:16,020
not equal we can simplify our program.
What I'm going to do is change the first

29
00:02:16,020 --> 00:02:20,820
condition on line 6, to check if the
guess is not equal to the answer.

30
00:02:20,820 --> 00:02:24,180
Now so that you can compare the new code with
what we've got at the moment,

31
00:02:24,180 --> 00:02:30,180
I'm going to comment out lines 6 through 21, and  put the new code before it. In the JetBrains

32
00:02:30,180 --> 00:02:34,230
IDEs you can comment out a block of code
by using control / or command slash on

33
00:02:34,230 --> 00:02:41,580
a Mac. So I'm going to select lines 6
through 21, control slash in my case,

34
00:02:41,580 --> 00:02:45,930
because I'm on Windows. Now I'm not aware
how to do, have a way to do that in IDLE.

35
00:02:45,930 --> 00:02:50,980
You may have to edit each line
individually if you're using your IDLE.

36
00:02:51,620 --> 00:02:55,040
Now comments are ignored by the Python interpreter, and they're there to

37
00:02:55,040 --> 00:02:59,840
document your code. In this case, the
comments prevent the code from being executed.

38
00:02:59,840 --> 00:03:02,680
Generally, you should use them
to explain when you've learned you've

39
00:03:02,690 --> 00:03:06,280
done something in a certain way, or as a
reminder of certain conditions or

40
00:03:06,280 --> 00:03:11,590
variable states that may not be
obvious by looking at the code.

41
00:03:11,590 --> 00:03:15,200
Remember that you may come back to
modify code, weeks or months, or even

42
00:03:15,200 --> 00:03:20,330
years after you wrote it. Documentation
can be extremely helpful when you work

43
00:03:20,330 --> 00:03:26,400
on another programmers code for the first time, or come back to your own code after a gap.

44
00:03:26,430 --> 00:03:30,420
In Python, comments, as you can see, start
with the hash symbol, and they actually

45
00:03:30,420 --> 00:03:34,230
can appear in a line on their own, or at
the end of a line of code. You can't

46
00:03:34,230 --> 00:03:37,769
place them in the middle of a line,
though. The hash inside the string is

47
00:03:37,769 --> 00:03:41,640
treated just like any other character,
and doesn't include a comment. If you

48
00:03:41,640 --> 00:03:46,760
want a comment to span several lines,
and start each line with a hash, as I've done here.

49
00:03:46,760 --> 00:03:49,590
Alright, so let's now start typing in some new code. So I'm going to come up

50
00:03:49,590 --> 00:03:56,880
to the top here, and specifically on line
6, I'm going to start typing, if guess

51
00:03:56,880 --> 00:04:02,160
is not equal, using the not equal 
comparison operator answer :

52
00:04:02,160 --> 00:04:09,040
and the next line, line 7, I'm going
to do, if guess is less than answer :

53
00:04:09,040 --> 00:04:15,960
print parentheses double quotes and
Please guess higher, in those double quotes.

54
00:04:15,960 --> 00:04:18,660
Otherwise I'm going to do an else,
noting that I've gone, indented back

55
00:04:18,660 --> 00:04:23,920
at the same level as the if on line 7,
else : and I'm going to put a comment here;

56
00:04:23,920 --> 00:04:33,980
hash guess must be greater than
answer. I'm going to print, or type in print,

57
00:04:33,980 --> 00:04:40,600
parentheses double quotes
Please guess lower. Then on the next line,

58
00:04:40,600 --> 00:04:45,000
I'm going to go back a level again, so at
the same level as the else now,

59
00:04:45,000 --> 00:04:51,120
guess equals int left parenthesis input left and right parenthesis and closing right parenthesis

60
00:04:51,120 --> 00:04:55,920
for our guess.
Then I'm going to type if guess is equal to

61
00:04:55,920 --> 00:05:02,159
answer, so two equal signs, colon
print parentheses double quotes and

62
00:05:02,159 --> 00:05:05,720
Well done, you guessed it, in double quotes.

63
00:05:06,460 --> 00:05:13,100
else, again at the same level as the if,
colon print parentheses double quotes,

64
00:05:13,100 --> 00:05:22,220
Sorry, you have not guessed correctly,
correctly, and then I'm gonna go back,

65
00:05:22,220 --> 00:05:25,700
back again, so we're all the way to the
left margin, again lined up with the if

66
00:05:25,700 --> 00:05:34,580
on line 6, else colon print
parentheses double quotes, You got it first time.

67
00:05:34,580 --> 00:05:38,300
Okay, so there's our code. So
once again this highlights the

68
00:05:38,310 --> 00:05:43,350
importance of indentation in Python.
Our second guess on line 11 is at the

69
00:05:43,350 --> 00:05:49,020
same indentation level as the if and
else statements on lines 7 and 9.

70
00:05:49,020 --> 00:05:53,520
It's not dependent on either condition, in other
words. We get a different message if

71
00:05:53,520 --> 00:05:59,310
guess is greater than 5 or less than 5,
but line 11 is executed, regardless of

72
00:05:59,310 --> 00:06:05,100
the outcome of the if-else clauses. I've also added a comment to the code on line 9.

73
00:06:05,100 --> 00:06:08,400
This makes it clear to any humans
reading the code, what's happening. If you

74
00:06:08,400 --> 00:06:11,430
were to read the inner block in
isolation, it may not be obvious that the

75
00:06:11,430 --> 00:06:16,700
guess must be greater than 5, rather than
a greater than equal to, i.e. not less than 5.

76
00:06:16,700 --> 00:06:20,420
The comment draws attention to the fact
that we would not have gotten to the

77
00:06:20,430 --> 00:06:24,600
inner block if guess as equal to 5. You
can often do the same thing in many

78
00:06:24,600 --> 00:06:28,980
different ways, and this is an example of
doing that. This new code does exactly

79
00:06:28,980 --> 00:06:32,360
the same thing as the previous code -
that's the code now that has been

80
00:06:32,360 --> 00:06:38,540
commented out on lines 19 through 34, and I'm gonna take the opportunity to remove our breakpoint there.

81
00:06:38,540 --> 00:06:40,700
Now this can often confuse new programmers

82
00:06:40,700 --> 00:06:43,820
because there's usually more
than one way to do the same thing.

83
00:06:43,820 --> 00:06:47,580
Both of these bits of code will produce
exactly the same results when given the

84
00:06:47,580 --> 00:06:53,730
same inputs. Of course, the code on lines 19 through 34 won't do anything now because they

85
00:06:53,730 --> 00:06:57,120
start with a hash. In other words, it's
been commented out. They're ignored when

86
00:06:57,120 --> 00:07:00,870
we run the program, but if they weren't
commented out, they'd produce the same

87
00:07:00,870 --> 00:07:05,370
results as the code we've now got on
lines 6 through 17. So let's check that

88
00:07:05,370 --> 00:07:09,660
by running the program to make sure that
it still works. Alright, so I'm going to

89
00:07:09,660 --> 00:07:13,500
use the same guesses that I used in the
previous video. The first guess was 9,

90
00:07:13,500 --> 00:07:16,600
so I'm going to do that again, and we get
the message

91
00:07:16,600 --> 00:07:22,780
Please guess lower. When I enter 5 as the
second guess, Well done, you guessed it.

92
00:07:22,780 --> 00:07:28,540
We're going to run the program again and
use 1 as our first guess.

93
00:07:28,540 --> 00:07:33,300
So Please guess higher is outputted to the screen. 5 is
my second guess, Well done you guessed it.

94
00:07:33,300 --> 00:07:38,900
So that's still working. Run it one more
time, run the program again, 1.

95
00:07:38,900 --> 00:07:44,540
This time we're gonna enter 9 - Sorry you have not
guessed correctly. So that's working fine

96
00:07:44,540 --> 00:07:49,300
and produces exactly the same results as
the previous, now commented out, code.

97
00:07:49,300 --> 00:07:52,960
You may also want to set breakpoints and
run the code in the debugger.

98
00:07:52,960 --> 00:07:56,700
Remember to think about which lines
should be executed before stepping over

99
00:07:56,700 --> 00:08:00,100
the conditions to verify your results.
Alright, so I'm going to finish this

100
00:08:00,100 --> 00:08:06,340
video now with a challenge and we'll go
over the solution to the challenge in the next video.

101
00:08:06,340 --> 00:08:11,880
So the challenge is to change the
condition on line 6 to, if guess is equal

102
00:08:11,880 --> 00:08:16,680
to answer, and you saw that previously it
was not equal to answer. So change it so

103
00:08:16,680 --> 00:08:20,400
it says if guess is equal to answer, and
then change the program in the

104
00:08:20,400 --> 00:08:24,480
appropriate places to give the correct
results. Alright, so that's the challenge.

105
00:08:24,480 --> 00:08:28,080
See how you go with the challenge and we'll
go over the solution to the challenge

106
00:08:28,080 --> 00:08:30,920
in the next video.

