1
00:00:01,980 --> 00:00:03,150
In this exercise,

2
00:00:03,150 --> 00:00:07,380
the goal is to write a program that adds the digits

3
00:00:07,380 --> 00:00:10,020
in a two-digit number.

4
00:00:10,020 --> 00:00:12,390
So for example, right now you should see that

5
00:00:12,390 --> 00:00:17,390
in the input pane there is a number and it is 39.

6
00:00:17,790 --> 00:00:22,790
Now, after it gets processed by your code inside main.py,

7
00:00:23,370 --> 00:00:27,510
your code should print out the value 3+9,

8
00:00:27,510 --> 00:00:29,313
which is equal to 12.

9
00:00:30,150 --> 00:00:33,210
Figure out how you can do this using what you've learned

10
00:00:33,210 --> 00:00:36,750
about data types, inputs and the print function.

11
00:00:36,750 --> 00:00:39,381
And once you think your code is doing

12
00:00:39,381 --> 00:00:41,220
what it's supposed to,

13
00:00:41,220 --> 00:00:43,803
click on the submit button to check your code.

14
00:00:49,230 --> 00:00:51,750
Now we have to do this in a few steps.

15
00:00:51,750 --> 00:00:55,439
So I'm going to walk you step by step through each of them.

16
00:00:55,439 --> 00:00:59,160
Now, how can we get hold of the first digit

17
00:00:59,160 --> 00:01:02,910
and the second digit separately from the input?

18
00:01:02,910 --> 00:01:05,310
Well, one thing you could have tried is you

19
00:01:05,310 --> 00:01:08,250
could have used type checking to check what is

20
00:01:08,250 --> 00:01:12,630
the data type that I'm getting through this input function?

21
00:01:12,630 --> 00:01:15,843
What is it that I can get hold of?

22
00:01:16,770 --> 00:01:18,270
And if you tried that, you see

23
00:01:18,270 --> 00:01:23,163
that the input comes in in the data type of a string.

24
00:01:24,270 --> 00:01:26,250
What we know about strings is

25
00:01:26,250 --> 00:01:29,910
that we saw that we can use a square bracket

26
00:01:29,910 --> 00:01:33,205
after a string and put in the position

27
00:01:33,205 --> 00:01:36,753
of the character in the string that we want to access.

28
00:01:39,240 --> 00:01:44,130
If we get the two-digit number variable, add square brackets [ ]

29
00:01:44,130 --> 00:01:48,120
afterwards, and put in 0 as the index,

30
00:01:48,120 --> 00:01:51,900
so get the first character out of that string<

31
00:01:51,900 --> 00:01:56,313
then we'd end up with the first number, but as a string.

32
00:01:57,690 --> 00:02:00,990
So the next part is converting that string

33
00:02:00,990 --> 00:02:05,130
into a different data type, a number data type.

34
00:02:05,130 --> 00:02:07,830
And in this case, because it's a whole number that we want,

35
00:02:07,830 --> 00:02:12,090
we can simply use the int() function and then wrap

36
00:02:12,090 --> 00:02:15,900
that-two digit number, accessing the first digit,

37
00:02:15,900 --> 00:02:18,960
into that function, like this.

38
00:02:18,960 --> 00:02:21,970
So now we have the first digit as a number

39
00:02:23,130 --> 00:02:25,530
and we can do the same thing to get hold

40
00:02:25,530 --> 00:02:28,560
of the second digit, but this time accessing

41
00:02:28,560 --> 00:02:32,010
that variable two-digit number at position 1,

42
00:02:32,010 --> 00:02:34,083
which will get us the second number.

43
00:02:35,070 --> 00:02:37,890
And you can print these out just to verify

44
00:02:37,890 --> 00:02:41,190
and you can check the data type of the variable first digit

45
00:02:41,190 --> 00:02:43,980
and second digit, just to make sure that we are now

46
00:02:43,980 --> 00:02:47,730
getting hold of a number, which can be multiplied,

47
00:02:47,730 --> 00:02:51,510
subtracted, added, whatever it is you want to do with it.

48
00:02:51,510 --> 00:02:55,350
Finally, we can add the two digits together

49
00:02:55,350 --> 00:02:58,170
adding first digit to the second digit

50
00:02:58,170 --> 00:02:59,913
and then printing it out.

51
00:03:00,990 --> 00:03:03,360
Now of course you can write this program

52
00:03:03,360 --> 00:03:05,220
in a lot fewer lines.

53
00:03:05,220 --> 00:03:08,100
You can write it all in one line if you wanted to.

54
00:03:08,100 --> 00:03:10,470
But what I want you to remember is always think

55
00:03:10,470 --> 00:03:12,060
about readability.

56
00:03:12,060 --> 00:03:14,400
If somebody else came and read your code,

57
00:03:14,400 --> 00:03:17,880
would it be easily understood by that person

58
00:03:17,880 --> 00:03:19,170
or would they have to think

59
00:03:19,170 --> 00:03:22,020
and spend a lot of time in order to figure it out?

60
00:03:22,020 --> 00:03:25,350
Don't optimize for the least number of lines.

61
00:03:25,350 --> 00:03:28,860
It's called Code Golf, and it's not something to aspire to.

62
00:03:28,860 --> 00:03:32,130
Instead, aspire to make the most readable code

63
00:03:32,130 --> 00:03:35,030
and you'll have the happiest colleagues who work with you.

