In the last lesson, we managed to get our countdown mechanism to work,
and we got it to count down from five minutes to zero. Now, everything works,
but there's just something a little bit niggling and you'll see it when I hit
start. Notice how it says 5:0.
Wouldn't it be so much better if it said 5:00?
So how would we do that? Well,
that is the zero comes from this count_sec because it's taking the count in
seconds, dividing it by 60 and finding the remainder using the modulo.
Now, when there is no remainder, it's just going to be equal to zero.
So we could use an if statement and check well
if the count_sec is equal to zero,
then instead of making it zero, let's set it to equal a string
which is "00".
Now this actually, believe it or not, works.
And you'll see it when I run it.
When I hit start, doesn't that look so much better?
But some of you might've realized that there's something a little bit crazy going on
here in these two lines of code.
We're checking to see if this variable holds data that is equal to the integer,
the whole number zero,
and then we're setting it to a string.
So these are completely two different data types.
That's an int and that is a string.
So how is it that we're able to do this?
So let me pull up the Python console.
We know that if we took a integer,
say 3, and we try to add it to a string, say a 4,
this is not going to work
and we get a type error because this is type int
and this is type string.
So you can't actually add these two together because there's a mismatch.
Now here's the crazy thing.
Let's create a variable a and let's make it hold the integer with 3.
And at this point, you can see that this a variable has a data type of int.
If I decide to change that variable and make it hold a different type of data,
let's say the number 4, or let's make it simpler,
let's make it hold the piece of text, Hello. As soon as that goes through,
you can see that a is now of data type string.
And the very fact that I'm able to do that,
change a variable's data type by changing the content in that variable,
this is what's known as dynamic typing. In this sense,
Python is a language that's quite unique because on one hand it is strongly
typed, in the sense that it holds on to the data type of the variable,
so this a is a string and it knows that it's a string
and if you do something that is not meant for a string,
say if you try to raise a to the power of 2,
then that is going to be a type error. Types matter
and when you call a function that's expecting a particular type,
say if you use the power function
which is expecting a integer and you pass in something
that's obviously not an integer say, our string here
and you try to get it to do some things, say raise it to the power of 3,
then again, we get type error.
It knows what type is supposed to go into that function
and it makes sure that you comply with that.
This is where the language is strongly typed.
But the part where the language is dynamically typed is that even though it
knows what type a variable is,
it knows what type a function expects,
you can also dynamically change the data type of any variable like this.
That is basically what we're using here. We're using dynamic
typing to change
the data type of count_sec from a integer to a string.
And then we use that string to display inside this canvas text.
Now there is however,
still a bit of a bug with this and you'll see it once it dips below 10 seconds.
Notice that as soon as it goes to 9 seconds,
instead of saying 09, which is what you would want or 08,
06,
it just lists the number of seconds until it reaches 00.
So how can we fix this?
The key lies in these two lines of code.
See if you can do this as a challenge,
pause the video and see if you can make it so that when it's nine seconds
remaining,
it says 09 or 08 or 07 and also 00 of course.
Pause the video and see if you can complete this challenge. All right.
So basically what we want to check for is when the count in seconds is less
than 10, because now it's going to be in the single digits
and instead of having just one digit,
we wanna add a zero in front of it.
So the way we can do this is through just an f-string.
We can use an f-string to add the zero in front and then add the count in
seconds afterwards,
formatting that into the standard kind of time format.
Now that we fixed that if we run our app again
it goes down to 10 seconds
you can see it now says 09, 08, 07
and it's nicely formatted using our f-string.
And we're able to do because of the flexibility of the data types in Python,
checking it as a number,
making sure it's less than 10 and then formatting it as a string and using the
f-string.
Now, this is not a feature that's available in all programming languages.
For example, languages like C or Java or Swift,
you won't be able to do this.
Once you create a variable and you give it a certain type of data,
then it must forever hold on to that type of data.
But Python allows you to change the data type of a variable just by assigning it
to a different type of value.
There's a really well-worded Stack Overflow answer that addresses this exact
thing
I'm talking about, talking about the fact that Python is a strongly dynamically
typed language. And I recommend that if you want to dig more into this,
to have a read of this answer and I'll link to this in the course resources.