Welcome back.
So up until now, we've learned about integers, floats, strings and boolean values, so let's try
and tie some of those things together.
Let's imagine your Facebook.
How do you think Facebook works underneath the hood when it comes to your profile?
Well, they would have, let's say, a variable called name, and this will have your name.
And then they'll probably have a variable with your age, let's say fifty and not actually 50, but
I'm pretending to be for this case just so I sound wiser and then let's say relationship status.
So, again, we also have relationship status on Facebook.
And for now, let's say single.
So that's my Facebook profile right there now, if we wanted to perhaps change that, let's say my relationship
status all of a sudden goes from single to it's complicated.
Well, that's an easy fix.
We would just do relationship status equals to.
It's complicated, I remember because of the single quote, we'll have to do backslash.
Complicated.
And now if I print my relationship status.
You'll see that it's been updated to it's complicated, I am no longer single, hooray!
But this is pretty simple, let's do something more complicated and this is going to be a fun exercise,
let's create a program that I guess is your age.
Let's use the input method that we've seen before.
Remember, the input method allows us to type something.
In here and receive it as input so we can assign that to a variable, so let's say this variable is
going to be birth year and that's going to equal the input that's going to ask.
When or what year were you?
Now, from here, I want you to pause the video and try to solve this yourself, how can you make it
so that at the end of this program you're going to get asked what year, where you're born, you're
going to type in your year, and then it's going to print out your age is whatever the ages.
So pause the video, try and solve this on your own.
Mind you, there is a bit of a trick in here.
So if he gets stuck, try to Google around and figure out what the issue may be.
Again, part of being a programmer is trying to solve these problems that you don't know the answers
to.
All right, let me show you my solution.
We have our birth year here that we're going to store in a variable.
And now we're going to calculate the age, I'm going to say age equals well, it's going to equal the
current year, let's say twenty nineteen and then twenty nineteen here is going to get subtracted from
whatever the birth year that you entered.
So let's enter that.
And then we're simply going to say, print your age is.
And we want to use an F string here, so I'm going to add an F here.
And then simply say.
If I run this.
I get what year were you born?
I was born in 1986.
Hmm, we get an error, and if you try this yourself, you may have encountered this error, we get
something called a type error and there's lots of errors that you can get in Python.
Again, we have a section on air, so don't stress too much about it.
But if you read here, it says unsupported operand type S four minus Int and string.
Hmmm, let's try and debug this.
It looks like we're using minus on an integer and a string.
So let me print out here the type of birth year.
If I run this and let's enter in nineteen eighty six, I get class string and this is a little gotcha
because what input does is it asks you for an input.
But that input that I wrote one nine eight six, well that actually gets converted to a string and assigned
to birth year.
So what that means is that we're trying to subtract.
A number or a string from a number.
So how do we solve this issue?
Well, you'd have to turn this into an integer, and this is something that we saw previously, what
we need to do is to convert this into an integer with using the INT function.
So let's try that if I remove this and click run.
I'll say nineteen eighty six.
And look at that, your age is 33.
We've created a program that is able to tell your age based on the year that you were born.
Now, this may have been tricky to you, but it teaches an important concept in programming that is
sometimes you storing data into different data types and sometimes you need those different data types
to interact together.
So you'll see a lot of problems where you have to convert a data type and this is a very common one
where you take an input that's a string and then you convert it to something like an integer.
So he can perform a mathematical operation, and that's why if you remember, we gave you a list of
all our data types in Python, the fundamental data types, and all of them were blue because, well,
each one of these are actions are functions that we can use.
For example, I converted the birth year into integer, but I could have also converted this to a float,
for example, and if I run this, it would still work because Python knows that, well, we can use
integers and floats in mathematical operation, but you can see here that it's converted into a float.
Now, if we convert this into a boolean.
And I click run.
Your age is twenty eighteen, that's really confusing, right?
Well, that's because this gets turned into true and underneath the hood, python does something weird
where a true value is converted to one because boolean remember, it's one or zero, true or false.
All right.
Hopefully this didn't confuse you too much.
It's something that will encounter throughout the course.
So don't worry, we'll get more and more practice.
However, I hope you get to practice this a little bit to really understand what's going on, because
although this is a couple of lines of code, the principles that we use here, we're going to use throughout
bigger and bigger code bases.
So I guess I'll see you in the next one.
Bye bye.