In this exercise,
the goal is to write a program that adds the digits
in a two-digit number.
So for example, right now you should see that
in the input pane there is a number and it is 39.
Now, after it gets processed by your code inside main.py,
your code should print out the value 3+9,
which is equal to 12.
Figure out how you can do this using what you've learned
about data types, inputs and the print function.
And once you think your code is doing
what it's supposed to,
click on the submit button to check your code.
Now we have to do this in a few steps.
So I'm going to walk you step by step through each of them.
Now, how can we get hold of the first digit
and the second digit separately from the input?
Well, one thing you could have tried is you
could have used type checking to check what is
the data type that I'm getting through this input function?
What is it that I can get hold of?
And if you tried that, you see
that the input comes in in the data type of a string.
What we know about strings is
that we saw that we can use a square bracket
after a string and put in the position
of the character in the string that we want to access.
If we get the two-digit number variable, add square brackets [ ]
afterwards, and put in 0 as the index,
so get the first character out of that string<
then we'd end up with the first number, but as a string.
So the next part is converting that string
into a different data type, a number data type.
And in this case, because it's a whole number that we want,
we can simply use the int() function and then wrap
that-two digit number, accessing the first digit,
into that function, like this.
So now we have the first digit as a number
and we can do the same thing to get hold
of the second digit, but this time accessing
that variable two-digit number at position 1,
which will get us the second number.
And you can print these out just to verify
and you can check the data type of the variable first digit
and second digit, just to make sure that we are now
getting hold of a number, which can be multiplied,
subtracted, added, whatever it is you want to do with it.
Finally, we can add the two digits together
adding first digit to the second digit
and then printing it out.
Now of course you can write this program
in a lot fewer lines.
You can write it all in one line if you wanted to.
But what I want you to remember is always think
about readability.
If somebody else came and read your code,
would it be easily understood by that person
or would they have to think
and spend a lot of time in order to figure it out?
Don't optimize for the least number of lines.
It's called Code Golf, and it's not something to aspire to.
Instead, aspire to make the most readable code
and you'll have the happiest colleagues who work with you.