1
00:00:04,290 --> 00:00:08,138
Alright so in this video, we're going to
make a small change to our code and see

2
00:00:08,138 --> 00:00:12,640
how it handles numbers that a user
enters at the keyboard. I'm also going to

3
00:00:12,640 --> 00:00:16,570
take the opportunity to explain why we
have to initialize separators on line

4
00:00:16,570 --> 00:00:21,520
2. I'm going to start with that in fact.
Let's just remind ourselves what this

5
00:00:21,520 --> 00:00:26,289
program is doing by running it. I'm not
debugging it this time. We've got the output at

6
00:00:26,289 --> 00:00:29,230
the bottom of the screen there, and
basically the separators are printed

7
00:00:29,230 --> 00:00:33,160
out and the code produces a list of the
seven numeric values contained in the

8
00:00:33,160 --> 00:00:38,110
number variable. So that works fine. The
question we're looking at is, why have we

9
00:00:38,110 --> 00:00:43,120
bound separators to an empty string on
line 2? Well let's comment that line

10
00:00:43,120 --> 00:00:47,800
out and see what happens when we run it.
The first thing you'll notice is that

11
00:00:47,800 --> 00:00:51,940
we've got warnings wherever the value of
separators is used in the code. So line

12
00:00:51,940 --> 00:00:55,690
6 over here, is trying to take the
current value of separators and

13
00:00:55,690 --> 00:01:00,700
appending the char to it. The problem is
that Python has no idea what the current

14
00:01:00,700 --> 00:01:04,720
value of separators is, and that's because we
haven't bound separators to any value -

15
00:01:04,720 --> 00:01:09,880
it doesn't exist.  If we hover over that,
we can see name separators can be not

16
00:01:09,880 --> 00:01:14,840
defined. We need to create the separators
variable by attaching it to a value,

17
00:01:14,840 --> 00:01:18,960
and that's what we're doing, effectively, on
line 2. So if I run the program now

18
00:01:18,970 --> 00:01:24,789
with that line still commented out, you
can see we actually get a crash there,

19
00:01:24,789 --> 00:01:29,470
name separators is not defined. So
basically, we need to define our

20
00:01:29,470 --> 00:01:34,120
separators variable, by binding it to a
value before we try to use its value.

21
00:01:34,120 --> 00:01:38,080
So that's really why line 2 is necessary.
So I'd better uncomment that, I'll just

22
00:01:38,080 --> 00:01:42,640
undo that change to fix that, so that our
program doesn't crash anymore.

23
00:01:42,640 --> 00:01:47,560
Alright so let's see how we can handle this code
to get several values from the user.

24
00:01:47,560 --> 00:01:51,729
On line 1, what I'm going to do is change
this to get input from the user, instead

25
00:01:51,729 --> 00:01:54,280
of actually using a string literal that
we're currently doing. So I'm going to

26
00:01:54,280 --> 00:01:58,750
change it to be number equals input
parenthesis double quotes

27
00:01:58,750 --> 00:02:10,360
Please enter a series of numbers using
any separators you like colon and I better

28
00:02:10,360 --> 00:02:17,700
put my spelling head on, separators.
Okay, so if we run the program now,

29
00:02:18,060 --> 00:02:29,600
and for the first example let's go with
1 comma 2 3 semicolon 4 5 6 : 7 8 9 0 1 2

30
00:02:29,600 --> 00:02:32,400
and what I'll also do is take
the opportunity, I'll just go back and

31
00:02:32,410 --> 00:02:39,250
put a space after the first comma as well.
1 comma space 2 3 semicolon 4 5 6

32
00:02:39,250 --> 00:02:44,350
: 7 8 9 0 1 2, also noting here that all
the values I've entered have a

33
00:02:44,350 --> 00:02:46,959
different number of digits.
That's something that our slice

34
00:02:46,959 --> 00:02:51,580
wouldn't have been able to handle. So
when I press ENTER here, we get a list

35
00:02:51,580 --> 00:02:57,250
containing the 4 values 1, 23, 456, 789012

36
00:02:57,250 --> 00:03:00,610
Remember that we're only interested in
the for loop at the moment. Again, don't

37
00:03:00,610 --> 00:03:04,120
worry about understanding lines 10 and
11 at this time. I'm going to finish this

38
00:03:04,120 --> 00:03:07,660
video by making our program a bit more
useful. Instead of just printing the

39
00:03:07,660 --> 00:03:11,260
values, I'll get the program to add them
up and print out the total. Python's

40
00:03:11,260 --> 00:03:14,560
actually got a sum function built-in and
you can find a list of the built-in

41
00:03:14,560 --> 00:03:22,000
functions, the link I'm going to put
into a browser now. The link is also in

42
00:03:22,000 --> 00:03:26,830
the resources section. You can see sum,
which is the one we're going to use, but

43
00:03:26,830 --> 00:03:30,610
again, you can check that at your leisure.
Going back to the code, let's make a few

44
00:03:30,610 --> 00:03:35,230
changes. Firstly, I'm going to comment out
line 8, then I'm gonna come down to line

45
00:03:35,230 --> 00:03:39,640
11. We're going to change this to
calculate the sum. So to do that I'm

46
00:03:39,640 --> 00:03:44,560
going to add sum after the left
parenthesis, with a left parenthesis,

47
00:03:44,560 --> 00:03:48,549
and right at the end I'm going to add
another right parenthesis to make the

48
00:03:48,549 --> 00:03:54,190
line valid. If we run the code now, we've
got a useful calculator for adding up a

49
00:03:54,190 --> 00:03:59,170
series of numbers. So we could do exactly
the same as we did previously; 1 comma

50
00:03:59,170 --> 00:04:08,680
space 2 3 semicolon 4 5 6 colon 7 8 9 0
1 2. We've got a value there 789492,

51
00:04:08,680 --> 00:04:14,079
and run it again. You can use any
separator you want with the numbers,

52
00:04:14,079 --> 00:04:19,329
so let's try another one;
1 comma 2 space 3 semicolon 4 colon

53
00:04:19,329 --> 00:04:28,880
space five space 6 space 7 comma 8 : 9
space 10. Press enter,

54
00:04:28,880 --> 00:04:33,140
and you can see, we get the value now of 55. the
important bit about this code is the

55
00:04:33,150 --> 00:04:38,370
for loop on lines 4 through 6. That
iterates over the input and builds up a

56
00:04:38,370 --> 00:04:42,450
string containing anything that isn't a
numeric character. Because we're

57
00:04:42,450 --> 00:04:46,230
extracting the separators from the
user's input, we don't then have to care

58
00:04:46,230 --> 00:04:51,260
about what separator characters they're
using. This code is already quite robust.

59
00:04:51,260 --> 00:04:54,060
Have a go at entering all sorts of
input and you'll find it's very

60
00:04:54,060 --> 00:04:57,990
difficult to crash the program. It is
possible to crash it by holding down the

61
00:04:57,990 --> 00:05:01,650
control key, for example, but for normal
input, it should cope with anything

62
00:05:01,650 --> 00:05:05,100
you can type. Moving forward in the next
video, we'll look at iterating over

63
00:05:05,100 --> 00:05:10,400
another iterable type - the range. See you
in the next video.

