Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right, now
it's time to put what you've learned into practice by clicking on the data types
day 2.1 code challenge.
In this challenge,
the idea is that you are going to be given a random two-digit number,
so something like 39, and your code that you'll write
will be able to add the first digit to the second digit and end up with a
numerical result. So for example, if the input was 39,
then 3 + 9 would be equal to 12. So the output would be 12.
And if the input was 26,
then your code needs to print 8 to the console.
So you're going to need to think about type checking, type conversion,
as well as everything that you learned before.
And then you're going to write your code below this line without changing any of
these lines. So pause the video now and give this code challenge a go.
All right. So how did that go? Did you manage to do it? If not,
have a cup of tea or give yourself a little bit of a break,
think about something else and then come back to it and try it again.
If you've already
given it a good go and you want to see what the solution might be,
then follow along with me.
So the first thing we know is that we're going to have a variable
that's going to be equal to a two-digit number.
But remember that that could mean anything. That could mean an integer,
that could mean a floating-point number, that could even mean a number as a
string.
So it might be helpful to actually find out the data type of this two-digit
number that we get. And to do that, we of course use the type function, all
lowercase. And then in between the parentheses,
we add the thing that we want to check. So it's this variable.
And then let's go ahead and print the result into the console.
So now let's run our code and let's type a two-digit number,
say 56.
and we get the result that the type of this two-digit number is a string
because, of course, we're getting hold of this through the input function and the
user could be typing anything into this input, a string,
their name or a number.
So now that we know that this variable has the datatype string,
then you can think of a little bit about what we know we can do with strings.
We can subscript strings, right? So if we had a string like hello,
and we want it to get a hold of the first character, we could put zero there.
And when I print this,
we'll get H being selected out of this string.
So if we know that we can subscript strings to get a hold of a particular value
at a particular position just by using subscripting like this,
then we can do the same with our two-digit number.
So we could say two_digit_number, let's get hold of the first digit.
So let's call it
first_digit equals two_digit_number [
0],
and then the second_digit could be two_digit_number [1].
Now,
when I go ahead and print these two items,
first_digit and second_digit,
then you'll see that if I enter a number like 87,
then my first digit is 8 and my second digit is 7.
So now I can probably start thinking about adding those two digits together.
For example,
we can say that result equals first_digit plus second_digit.
But of course, when I run this right now and I print the value of result,
you'll see that for a number like 87
what I will get is just a eight concatenated to seven,
which turns it back into 87, which is not what I want at all.
Especially after all of my hard work separating out the digits.
What I want to do instead is to turn each of these,
which currently has the data type of string,
so I can demonstrate that to you by saying type of first_digit,
you can see it is a string.
So if I could only change first_digit and second_digit from string data types
into an integer data type by using the type conversion that we learned about
in the last lesson, well then once I do that,
then this plus sign has a whole different meaning because now it's going to add
this first_digit to the second_digit instead of concatenating it.
So now if I go ahead and print the results,
then you'll see that for the same number
87, my result is now 15.
I've now managed to achieve what the code exercise wants me to do.
So did you manage to get it right? If not,
go ahead and have a look at the model solution
and I've written some comments for each line of code that walks you through each
of the steps.
Now notice that instead of converting the second_digit here,
you can also do it up here, like what you see right here.
So I have a play around with the code,
especially if you struggled with this and didn't manage to get it right.
And then once you've understood what's going on here line by line,
if you need, have a look at the previous videos,
then go back to the code challenge and attempt it again.
And only once you're happy that you really understand everything that was
covered in this challenge,
then head over to the next lesson where we're going to talk more about
mathematical operators
like the plus sign and others that you can use in your code. So for all of that
and more, I'll see you on the next lesson.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.