1
00:00:05,550 --> 00:00:07,630
ok so lets talk about that sneaky

2
00:00:07,630 --> 00:00:09,780
method i mentioned it does involve

3
00:00:09,780 --> 00:00:11,230
thinking about things slightly

4
00:00:11,230 --> 00:00:13,480
differently and a clue again was in that

5
00:00:13,480 --> 00:00:15,160
comment that i added down here in line

6
00:00:15,160 --> 00:00:17,620
42 unless the final character in the

7
00:00:17,620 --> 00:00:19,750
string was a period so the question is

8
00:00:19,750 --> 00:00:22,170
what would happen if we made sure that

9
00:00:22,170 --> 00:00:23,760
the final character in the input string

10
00:00:23,760 --> 00:00:25,260
was a period

11
00:00:25,260 --> 00:00:27,420
well two things would happen the for loop

12
00:00:27,420 --> 00:00:29,410
would always execute at least once

13
00:00:29,410 --> 00:00:31,390
because there would be at least one

14
00:00:31,390 --> 00:00:34,110
character in the input string also would

15
00:00:34,110 --> 00:00:36,310
be no need for that final test because

16
00:00:36,310 --> 00:00:38,590
if the final character is a period than

17
00:00:38,590 --> 00:00:40,360
the previous segments details would

18
00:00:40,360 --> 00:00:42,280
always be printed so let's see how that

19
00:00:42,280 --> 00:00:43,930
would actually work

20
00:00:43,930 --> 00:00:45,370
first I'm going to start by commenting

21
00:00:45,370 --> 00:00:50,440
out this final if statement and

22
00:00:50,440 --> 00:00:51,430
also going to comment out the

23
00:00:51,430 --> 00:00:53,170
initialization to the character variable

24
00:00:53,170 --> 00:00:54,280
on line

25
00:00:54,280 --> 00:00:57,940
32 now if we're making sure that the for

26
00:00:57,940 --> 00:01:00,580
loop executes at-least once then we

27
00:01:00,580 --> 00:01:02,020
don't need to make sure the character

28
00:01:02,020 --> 00:01:03,400
has a value so that's why I'm commenting

29
00:01:03,400 --> 00:01:05,920
that out so at this point we really need

30
00:01:05,920 --> 00:01:08,580
to do is append a full stop

31
00:01:08,580 --> 00:01:11,050
sorry period to whatever the user types

32
00:01:11,050 --> 00:01:14,200
in so down here under line 28 i'm going

33
00:01:14,200 --> 00:01:18,790
to type....

34
00:01:18,790 --> 00:01:21,220
and again I won't run through all the tests

35
00:01:21,220 --> 00:01:23,080
because I've done that and with one

36
00:01:23,080 --> 00:01:24,730
exception they all produce exactly the

37
00:01:24,730 --> 00:01:27,070
same results I will make sure that running

38
00:01:27,070 --> 00:01:28,780
the program and entering blank input works

39
00:01:28,780 --> 00:01:32,260
so lets do that

40
00:01:32,260 --> 00:01:34,870
press enter we are now correctly getting the

41
00:01:34,870 --> 00:01:36,150
message segment one contains zero

42
00:01:36,150 --> 00:01:38,680
characters whereas before course we need

43
00:01:38,680 --> 00:01:41,970
that optional code on line 44 and 45 to

44
00:01:41,970 --> 00:01:44,800
get that working let's also try single

45
00:01:44,800 --> 00:01:46,720
segment I'm going to run it again and

46
00:01:46,720 --> 00:01:50,560
just enter 8 for the input segment one

47
00:01:50,560 --> 00:01:52,650
contains one character that also works

48
00:01:52,650 --> 00:01:56,320
and try...

49
00:01:56,320 --> 00:01:59,220
that's also working but the exception is

50
00:01:59,220 --> 00:02:01,510
any input that already ends in a period

51
00:02:01,510 --> 00:02:03,400
so if we actually run that again

52
00:02:03,400 --> 00:02:11,520
....and again a period on the

53
00:02:11,520 --> 00:02:12,980
end

54
00:02:12,980 --> 00:02:15,380
so you got segment 1234 which is

55
00:02:15,380 --> 00:02:17,600
correct but this modified program now

56
00:02:17,600 --> 00:02:19,310
displays the results for a fifth segment

57
00:02:19,310 --> 00:02:23,000
containing 0 characters now that's

58
00:02:23,000 --> 00:02:25,490
arguably more correct but if you want

59
00:02:25,490 --> 00:02:27,260
exactly the same behavior as we had

60
00:02:27,260 --> 00:02:29,450
previously then you just check that the

61
00:02:29,450 --> 00:02:31,010
input string didn't already end with a

62
00:02:31,010 --> 00:02:33,200
full stop then only trying to append one if it

63
00:02:33,200 --> 00:02:34,100
didn't

64
00:02:34,100 --> 00:02:35,540
the important thing here though is

65
00:02:35,540 --> 00:02:36,920
thinking about the problem slightly

66
00:02:36,920 --> 00:02:40,490
differently can lead to simple code now

67
00:02:40,490 --> 00:02:42,380
we haven't covered how to check the last

68
00:02:42,380 --> 00:02:44,300
character in the string so don't

69
00:02:44,300 --> 00:02:46,520
worry about that bit i'm going to make

70
00:02:46,520 --> 00:02:48,230
the change to check the final character

71
00:02:48,230 --> 00:02:51,140
just to satisfy the curious so let's go

72
00:02:51,140 --> 00:02:54,230
ahead and do that so up here we're

73
00:02:54,230 --> 00:02:55,880
going to change this code and put....

74
00:02:55,880 --> 00:02:59,720
....

75
00:02:59,720 --> 00:03:09,440
....

76
00:03:09,440 --> 00:03:11,630
....now we'll be covering

77
00:03:11,630 --> 00:03:13,820
that in the course soon if you run that

78
00:03:13,820 --> 00:03:14,540
again

79
00:03:14,540 --> 00:03:21,020
...we now

80
00:03:21,020 --> 00:03:23,030
correctly get the results and the fifth

81
00:03:23,030 --> 00:03:25,040
segment is now no longer showing that's

82
00:03:25,040 --> 00:03:27,710
because we're only adding the period if

83
00:03:27,710 --> 00:03:30,440
it is not already one there now before i

84
00:03:30,440 --> 00:03:31,660
finish this video there is one more

85
00:03:31,660 --> 00:03:33,470
thing that I want to mention and I'm

86
00:03:33,470 --> 00:03:35,210
sort of Crow barring this in here

87
00:03:35,210 --> 00:03:37,010
because it was covered in the previous

88
00:03:37,010 --> 00:03:39,860
version of this video and incidentally

89
00:03:39,860 --> 00:03:41,690
I re recorded this video and

90
00:03:41,690 --> 00:03:42,650
the previous one

91
00:03:42,650 --> 00:03:43,940
after listening to comments from

92
00:03:43,940 --> 00:03:45,980
students so I'm going to make a slight

93
00:03:45,980 --> 00:03:48,080
change to the code so that i can still

94
00:03:48,080 --> 00:03:50,270
cover this next bit so forgive me if

95
00:03:50,270 --> 00:03:52,730
this is a slightly contrived way to lead

96
00:03:52,730 --> 00:03:56,060
into it so what we are going to do is copy the

97
00:03:56,060 --> 00:03:58,700
description on IP address from the

98
00:03:58,700 --> 00:04:02,410
comment on line 4 here and paste it

99
00:04:02,410 --> 00:04:05,240
into the input prompt on line 28

100
00:04:05,240 --> 00:04:10,290
going to copy the entire line

101
00:04:10,290 --> 00:04:12,330
copy and I'm gonna replace it here

102
00:04:12,330 --> 00:04:18,720
on line 28 I just removed the word but

103
00:04:18,720 --> 00:04:20,910
for simplicity so i'll end up with a

104
00:04:20,910 --> 00:04:23,400
full stop like you can see that so what

105
00:04:23,400 --> 00:04:24,870
we've done is we've made the message a

106
00:04:24,870 --> 00:04:26,850
little bit more informative so that we

107
00:04:26,850 --> 00:04:28,350
can have a look at why we don't get a

108
00:04:28,350 --> 00:04:30,060
green tick at the top-right of our code

109
00:04:30,060 --> 00:04:32,520
window anymore and also take the

110
00:04:32,520 --> 00:04:33,690
opportunity to clear up some of these

111
00:04:33,690 --> 00:04:37,140
lines down the end just leave the one

112
00:04:37,140 --> 00:04:39,330
line down the bottom and we got our

113
00:04:39,330 --> 00:04:42,030
green tick now intelliJ tries to make

114
00:04:42,030 --> 00:04:44,160
life easy by highlighting all

115
00:04:44,160 --> 00:04:46,800
occurrences of variables etc so you may

116
00:04:46,800 --> 00:04:48,510
find that there are a lot of markers on

117
00:04:48,510 --> 00:04:49,920
the right-hand side of the code window

118
00:04:49,920 --> 00:04:51,780
for example if you click on the word

119
00:04:51,780 --> 00:04:53,910
segment on line 32 i'll do that now

120
00:04:53,910 --> 00:04:57,210
notice how we now got segment

121
00:04:57,210 --> 00:04:59,010
showing in a number we got the

122
00:04:59,010 --> 00:05:00,360
markers on the right-hand side now

123
00:05:00,360 --> 00:05:02,880
that's because intelliJ will highlight

124
00:05:02,880 --> 00:05:05,490
all instances of segments in the code

125
00:05:05,490 --> 00:05:07,290
and place a marker in the right margin

126
00:05:07,290 --> 00:05:09,180
and the reason for that is that it

127
00:05:09,180 --> 00:05:10,800
allows you to quickly see the code for

128
00:05:10,800 --> 00:05:12,690
each instance by hovering over the

129
00:05:12,690 --> 00:05:14,790
marker or to jump to any occurence by

130
00:05:14,790 --> 00:05:17,370
clicking on the marker now click into a

131
00:05:17,370 --> 00:05:18,930
blank line to clear the marker so I'll do

132
00:05:18,930 --> 00:05:22,230
that now and what I need to do for this

133
00:05:22,230 --> 00:05:23,580
error to come up because I copy and

134
00:05:23,580 --> 00:05:26,340
pasted but what I didn't do was add another

135
00:05:26,340 --> 00:05:28,290
or leave the original message their

136
00:05:28,290 --> 00:05:31,620
so going to type that again so you

137
00:05:31,620 --> 00:05:33,270
can see this problem

138
00:05:33,270 --> 00:05:37,140
alright so you can see what's happened

139
00:05:37,140 --> 00:05:39,650
now that I've made it extra long this is

140
00:05:39,650 --> 00:05:41,040
was the original error that i wanted to

141
00:05:41,040 --> 00:05:43,710
show you note that we no longer got

142
00:05:43,710 --> 00:05:45,150
a green tick in the box in the top

143
00:05:45,150 --> 00:05:47,220
right-hand corner and just to confirm

144
00:05:47,220 --> 00:05:49,400
if i come over and select segment

145
00:05:49,400 --> 00:05:51,690
again you can see there's other errors

146
00:05:51,690 --> 00:05:53,520
again and if I just click back

147
00:05:53,520 --> 00:05:54,840
we still got an error showing over there

148
00:05:54,840 --> 00:05:57,510
and I can hover over that line too long

149
00:05:57,510 --> 00:05:59,610
and 130 is greater than a 120

150
00:05:59,610 --> 00:06:02,400
characters and I clicked on the warning

151
00:06:02,400 --> 00:06:05,150
that you take me automatically to

152
00:06:05,150 --> 00:06:06,300
line to show where the problem is

153
00:06:06,300 --> 00:06:08,700
you can see it's got to line 28 and

154
00:06:08,700 --> 00:06:10,380
obviously it's pretty clear on the fact

155
00:06:10,380 --> 00:06:11,640
that that's underlined that that's where

156
00:06:11,640 --> 00:06:12,450
the problem is

157
00:06:12,450 --> 00:06:14,430
so the problem here is that the Python

158
00:06:14,430 --> 00:06:16,620
parser doesn't have a 120

159
00:06:16,620 --> 00:06:18,770
character restriction because if it did

160
00:06:18,770 --> 00:06:20,730
work our code will execute and we can run

161
00:06:20,730 --> 00:06:23,880
this its still going to actually work

162
00:06:23,880 --> 00:06:29,640
the code obviously still work so the

163
00:06:29,640 --> 00:06:31,290
problem is that isn't that the pyhon

164
00:06:31,290 --> 00:06:33,060
parcer can't handle something greater

165
00:06:33,060 --> 00:06:36,060
than 120 characters it's just that the

166
00:06:36,060 --> 00:06:38,190
python standards do recommend a maximum

167
00:06:38,190 --> 00:06:40,590
length which used to be 79 characters

168
00:06:40,590 --> 00:06:42,780
now the reason is largely down to

169
00:06:42,780 --> 00:06:44,820
readability especially when coding in

170
00:06:44,820 --> 00:06:45,900
a terminal window

171
00:06:45,900 --> 00:06:48,120
now google actually specify a maximum

172
00:06:48,120 --> 00:06:50,250
length of 80 characters with a few

173
00:06:50,250 --> 00:06:52,980
exceptions and let me bring the link up

174
00:06:52,980 --> 00:06:56,790
on the screen quickly so you can read

175
00:06:56,790 --> 00:06:58,230
more about that on that link that I just pasted

176
00:06:58,230 --> 00:07:00,570
in but to also if you want

177
00:07:00,570 --> 00:07:02,910
to have a full half argument google

178
00:07:02,910 --> 00:07:05,130
python line length and you can really

179
00:07:05,130 --> 00:07:07,800
become embroiled in much utility the

180
00:07:07,800 --> 00:07:09,480
time is much better spent coding than

181
00:07:09,480 --> 00:07:12,030
worrying about such details the bottom

182
00:07:12,030 --> 00:07:13,380
line here is not to be too concerned

183
00:07:13,380 --> 00:07:15,720
about whether it's 79 characters or its

184
00:07:15,720 --> 00:07:17,670
a 120 whether it's 80 but

185
00:07:17,670 --> 00:07:19,080
let's actually go back and appease

186
00:07:19,080 --> 00:07:21,150
IntelliJ so that we're not distracted by

187
00:07:21,150 --> 00:07:23,610
warnings in that right margin i'm just

188
00:07:23,610 --> 00:07:24,840
going to go back to intellij again

189
00:07:24,840 --> 00:07:28,680
we can actually fix it pretty easily we

190
00:07:28,680 --> 00:07:30,570
can place the cursor before the words

191
00:07:30,570 --> 00:07:34,950
separated here and press enter and you

192
00:07:34,950 --> 00:07:35,730
can see that

193
00:07:35,730 --> 00:07:37,710
intellij correctly fixes the problem for

194
00:07:37,710 --> 00:07:40,500
us because it's split the line and sort

195
00:07:40,500 --> 00:07:42,420
out the quotes for us automatically so

196
00:07:42,420 --> 00:07:45,150
that still valid Python syntax so you'll

197
00:07:45,150 --> 00:07:47,250
often see long lines especially text

198
00:07:47,250 --> 00:07:49,230
strings like this one spilled out of two

199
00:07:49,230 --> 00:07:51,870
or more lines in the code now keep in

200
00:07:51,870 --> 00:07:53,340
mind that python also has a line

201
00:07:53,340 --> 00:07:55,320
continuation character which is a

202
00:07:55,320 --> 00:07:57,480
backslash let's have a look at how that

203
00:07:57,480 --> 00:07:58,170
works

204
00:07:58,170 --> 00:08:01,080
we're going to actually cut that

205
00:08:01,080 --> 00:08:02,820
text I'm just going to undo that change

206
00:08:02,820 --> 00:08:06,690
but and undo which was a command z

207
00:08:06,690 --> 00:08:09,030
on a Mac and control z on a pc or

208
00:08:09,030 --> 00:08:12,210
linux machine i'm going to copy that

209
00:08:12,210 --> 00:08:16,530
entire input i'm going to actually put

210
00:08:16,530 --> 00:08:18,240
it into a variable so I'm going to just

211
00:08:18,240 --> 00:08:19,620
type...

212
00:08:19,620 --> 00:08:22,620
....

213
00:08:22,620 --> 00:08:24,840
...

214
00:08:24,840 --> 00:08:27,720
and unsurprisingly we're getting an

215
00:08:27,720 --> 00:08:30,210
error on that line as well indicating

216
00:08:30,210 --> 00:08:32,400
that the lines too long so we can

217
00:08:32,400 --> 00:08:34,620
split in the same way i can position the

218
00:08:34,620 --> 00:08:37,500
cursor just before separated and press

219
00:08:37,500 --> 00:08:40,110
enter that fixes the problem at least

220
00:08:40,110 --> 00:08:42,840
for the input prompt variable but note

221
00:08:42,840 --> 00:08:44,310
that this time because we've had in a

222
00:08:44,310 --> 00:08:46,350
string that fixed it by adding this

223
00:08:46,350 --> 00:08:48,750
backslash at the end of the first line

224
00:08:48,750 --> 00:08:50,550
so if you want to split a line you can

225
00:08:50,550 --> 00:08:52,840
use the you can use a backslash

226
00:08:52,840 --> 00:08:54,910
character just like intelliJ has done

227
00:08:54,910 --> 00:08:57,720
here and I can fix the next line

228
00:08:57,720 --> 00:09:02,370
now by changing that to input....

229
00:09:02,370 --> 00:09:04,740
just confirming that program

230
00:09:04,740 --> 00:09:10,500
still runs and still getting the outputs

231
00:09:10,500 --> 00:09:11,490
so still working

232
00:09:11,490 --> 00:09:13,900
so again if you want to split a line you

233
00:09:13,900 --> 00:09:15,540
can use that backslash character to do

234
00:09:15,540 --> 00:09:17,250
that just like intellij done here

235
00:09:17,250 --> 00:09:20,590
on line 28 unfortunately many style

236
00:09:20,590 --> 00:09:23,610
guides including Google's specify that

237
00:09:23,610 --> 00:09:25,470
you shouldn't use the line continuation

238
00:09:25,470 --> 00:09:28,110
character unless theirs no alternative

239
00:09:28,110 --> 00:09:29,550
so you may actually find strings like

240
00:09:29,550 --> 00:09:32,350
ours enclosed in parentheses instead to

241
00:09:32,350 --> 00:09:33,450
show you how that works i'm going to

242
00:09:33,450 --> 00:09:37,500
delete the parentheses like so we go

243
00:09:37,500 --> 00:09:39,280
back to the start of the line and add our

244
00:09:39,280 --> 00:09:42,280
left parentheses then go to the

245
00:09:42,280 --> 00:09:44,850
right of the line second line

246
00:09:44,850 --> 00:09:47,250
right parenthesis the other thing you

247
00:09:47,250 --> 00:09:48,930
need to do is make sure that these are

248
00:09:48,930 --> 00:09:51,540
lined up correctly and once I've line them up

249
00:09:51,540 --> 00:09:52,840
correctly you can see that warning

250
00:09:52,840 --> 00:09:55,230
actually disappears but without doing

251
00:09:55,230 --> 00:09:57,600
that you'll get a weird error here about

252
00:09:57,600 --> 00:09:59,290
continuation line under indented

253
00:09:59,290 --> 00:10:01,470
for vision indent so just make sure

254
00:10:01,470 --> 00:10:03,600
thats the double quotes are

255
00:10:03,600 --> 00:10:05,910
basically in line so I've mentioned this

256
00:10:05,910 --> 00:10:07,470
so that you can make sense of code

257
00:10:07,470 --> 00:10:10,230
written by other programmers a line can

258
00:10:10,230 --> 00:10:11,700
be broken using that backslash

259
00:10:11,700 --> 00:10:13,740
continuation character but most

260
00:10:13,740 --> 00:10:15,910
expressions can also be split without

261
00:10:15,910 --> 00:10:18,180
a line continuation character if the

262
00:10:18,180 --> 00:10:20,880
expression is enclosed in parentheses as

263
00:10:20,880 --> 00:10:22,860
it is in the example now for the input

264
00:10:22,860 --> 00:10:24,450
_prompt variable on-screen

265
00:10:24,450 --> 00:10:27,420
some style guides such as Google suggest

266
00:10:27,420 --> 00:10:29,290
you use parentheses instead of the

267
00:10:29,290 --> 00:10:31,560
backslash whenever possible and that's

268
00:10:31,560 --> 00:10:33,510
why IntelliJ didn't insert the backslash

269
00:10:33,510 --> 00:10:35,410
when the string was included as an

270
00:10:35,410 --> 00:10:37,800
argument to the input function earlier

271
00:10:37,800 --> 00:10:39,930
on line 30 because it was enclosed in

272
00:10:39,930 --> 00:10:41,910
parentheses the backslash actually

273
00:10:41,910 --> 00:10:43,150
wasn't necessary

274
00:10:43,150 --> 00:10:45,240
alright so that's it that's the end of

275
00:10:45,240 --> 00:10:46,420
our challenge and I hope you've got a

276
00:10:46,420 --> 00:10:47,880
lot out of it and let's move on to the

277
00:10:47,880 --> 00:10:53,360
next video

