1
00:00:00,780 --> 00:00:04,830
In the last lesson, we managed to get our countdown mechanism to work,

2
00:00:05,160 --> 00:00:10,100
and we got it to count down from five minutes to zero. Now, everything works,

3
00:00:10,580 --> 00:00:14,510
but there's just something a little bit niggling and you'll see it when I hit

4
00:00:14,510 --> 00:00:18,200
start. Notice how it says 5:0.

5
00:00:18,740 --> 00:00:21,880
Wouldn't it be so much better if it said 5:00?

6
00:00:23,450 --> 00:00:25,280
So how would we do that? Well,

7
00:00:25,550 --> 00:00:30,320
that is the zero comes from this count_sec because it's taking the count in

8
00:00:30,320 --> 00:00:35,210
seconds, dividing it by 60 and finding the remainder using the modulo.

9
00:00:35,720 --> 00:00:39,140
Now, when there is no remainder, it's just going to be equal to zero.

10
00:00:39,620 --> 00:00:42,500
So we could use an if statement and check well

11
00:00:42,740 --> 00:00:45,800
if the count_sec is equal to zero,

12
00:00:46,190 --> 00:00:50,990
then instead of making it zero, let's set it to equal a string

13
00:00:51,020 --> 00:00:52,160
which is "00".

14
00:00:52,790 --> 00:00:56,150
Now this actually, believe it or not, works.

15
00:00:56,900 --> 00:00:58,310
And you'll see it when I run it.

16
00:00:58,340 --> 00:01:01,250
When I hit start, doesn't that look so much better?

17
00:01:02,330 --> 00:01:07,330
But some of you might've realized that there's something a little bit crazy going on

18
00:01:07,640 --> 00:01:09,380
here in these two lines of code.

19
00:01:09,830 --> 00:01:14,830
We're checking to see if this variable holds data that is equal to the integer,

20
00:01:16,610 --> 00:01:18,650
the whole number zero,

21
00:01:19,250 --> 00:01:21,890
and then we're setting it to a string.

22
00:01:22,310 --> 00:01:26,000
So these are completely two different data types.

23
00:01:26,030 --> 00:01:29,120
That's an int and that is a string.

24
00:01:29,540 --> 00:01:32,450
So how is it that we're able to do this?

25
00:01:33,350 --> 00:01:36,350
So let me pull up the Python console.

26
00:01:36,560 --> 00:01:39,260
We know that if we took a integer,

27
00:01:39,290 --> 00:01:44,180
say 3, and we try to add it to a string, say a 4,

28
00:01:44,480 --> 00:01:45,800
this is not going to work

29
00:01:45,830 --> 00:01:50,330
and we get a type error because this is type int

30
00:01:50,420 --> 00:01:52,250
and this is type string.

31
00:01:52,520 --> 00:01:57,520
So you can't actually add these two together because there's a mismatch.

32
00:01:58,250 --> 00:01:59,720
Now here's the crazy thing.

33
00:01:59,840 --> 00:02:04,550
Let's create a variable a and let's make it hold the integer with 3.

34
00:02:05,150 --> 00:02:10,150
And at this point, you can see that this a variable has a data type of int.

35
00:02:11,390 --> 00:02:16,130
If I decide to change that variable and make it hold a different type of data,

36
00:02:16,430 --> 00:02:19,400
let's say the number 4, or let's make it simpler,

37
00:02:19,400 --> 00:02:23,870
let's make it hold the piece of text, Hello. As soon as that goes through,

38
00:02:23,870 --> 00:02:27,410
you can see that a is now of data type string.

39
00:02:28,010 --> 00:02:30,950
And the very fact that I'm able to do that,

40
00:02:31,100 --> 00:02:36,100
change a variable's data type by changing the content in that variable,

41
00:02:37,310 --> 00:02:42,310
this is what's known as dynamic typing. In this sense,

42
00:02:42,770 --> 00:02:47,770
Python is a language that's quite unique because on one hand it is strongly

43
00:02:48,380 --> 00:02:53,380
typed, in the sense that it holds on to the data type of the variable,

44
00:02:54,230 --> 00:02:58,670
so this a is a string and it knows that it's a string

45
00:02:59,110 --> 00:03:02,650
and if you do something that is not meant for a string,

46
00:03:03,010 --> 00:03:06,640
say if you try to raise a to the power of 2,

47
00:03:07,090 --> 00:03:11,440
then that is going to be a type error. Types matter

48
00:03:11,800 --> 00:03:15,280
and when you call a function that's expecting a particular type,

49
00:03:15,610 --> 00:03:18,070
say if you use the power function

50
00:03:18,100 --> 00:03:22,600
which is expecting a integer and you pass in something

51
00:03:22,630 --> 00:03:26,980
that's obviously not an integer say, our string here

52
00:03:27,700 --> 00:03:32,020
and you try to get it to do some things, say raise it to the power of 3,

53
00:03:32,380 --> 00:03:34,300
then again, we get type error.

54
00:03:34,330 --> 00:03:37,510
It knows what type is supposed to go into that function

55
00:03:37,840 --> 00:03:40,570
and it makes sure that you comply with that.

56
00:03:40,600 --> 00:03:43,150
This is where the language is strongly typed.

57
00:03:43,660 --> 00:03:48,660
But the part where the language is dynamically typed is that even though it

58
00:03:48,700 --> 00:03:51,160
knows what type a variable is,

59
00:03:51,220 --> 00:03:54,100
it knows what type a function expects,

60
00:03:54,580 --> 00:03:59,580
you can also dynamically change the data type of any variable like this.

61
00:04:02,500 --> 00:04:06,130
That is basically what we're using here. We're using dynamic

62
00:04:06,130 --> 00:04:07,510
typing to change

63
00:04:07,510 --> 00:04:12,510
the data type of count_sec from a integer to a string.

64
00:04:13,210 --> 00:04:18,209
And then we use that string to display inside this canvas text.

65
00:04:19,390 --> 00:04:20,529
Now there is however,

66
00:04:20,529 --> 00:04:25,530
still a bit of a bug with this and you'll see it once it dips below 10 seconds.

67
00:04:27,430 --> 00:04:30,430
Notice that as soon as it goes to 9 seconds,

68
00:04:30,820 --> 00:04:34,660
instead of saying 09, which is what you would want or 08,

69
00:04:34,690 --> 00:04:35,523
06,

70
00:04:35,740 --> 00:04:40,740
it just lists the number of seconds until it reaches 00.

71
00:04:41,380 --> 00:04:43,690
So how can we fix this?

72
00:04:44,770 --> 00:04:47,050
The key lies in these two lines of code.

73
00:04:47,740 --> 00:04:49,810
See if you can do this as a challenge,

74
00:04:50,290 --> 00:04:55,290
pause the video and see if you can make it so that when it's nine seconds

75
00:04:55,390 --> 00:04:56,200
remaining,

76
00:04:56,200 --> 00:05:01,200
it says 09 or 08 or 07 and also 00 of course.

77
00:05:03,370 --> 00:05:07,840
Pause the video and see if you can complete this challenge. All right.

78
00:05:07,840 --> 00:05:12,700
So basically what we want to check for is when the count in seconds is less

79
00:05:12,700 --> 00:05:17,050
than 10, because now it's going to be in the single digits

80
00:05:17,230 --> 00:05:20,590
and instead of having just one digit,

81
00:05:20,650 --> 00:05:22,870
we wanna add a zero in front of it.

82
00:05:23,470 --> 00:05:26,890
So the way we can do this is through just an f-string.

83
00:05:27,340 --> 00:05:32,340
We can use an f-string to add the zero in front and then add the count in

84
00:05:32,620 --> 00:05:34,390
seconds afterwards,

85
00:05:34,600 --> 00:05:39,220
formatting that into the standard kind of time format.

86
00:05:40,270 --> 00:05:44,290
Now that we fixed that if we run our app again

87
00:05:44,590 --> 00:05:46,450
it goes down to 10 seconds

88
00:05:47,140 --> 00:05:51,190
you can see it now says 09, 08, 07

89
00:05:51,460 --> 00:05:54,430
and it's nicely formatted using our f-string.

90
00:05:55,660 --> 00:06:00,660
And we're able to do because of the flexibility of the data types in Python,

91
00:06:01,130 --> 00:06:02,480
checking it as a number,

92
00:06:02,510 --> 00:06:07,430
making sure it's less than 10 and then formatting it as a string and using the

93
00:06:07,430 --> 00:06:08,263
f-string.

94
00:06:08,900 --> 00:06:13,070
Now, this is not a feature that's available in all programming languages.

95
00:06:13,370 --> 00:06:17,090
For example, languages like C or Java or Swift,

96
00:06:17,420 --> 00:06:19,280
you won't be able to do this.

97
00:06:19,880 --> 00:06:24,470
Once you create a variable and you give it a certain type of data,

98
00:06:24,680 --> 00:06:27,830
then it must forever hold on to that type of data.

99
00:06:28,520 --> 00:06:33,520
But Python allows you to change the data type of a variable just by assigning it

100
00:06:34,820 --> 00:06:36,980
to a different type of value.

101
00:06:37,970 --> 00:06:42,800
There's a really well-worded Stack Overflow answer that addresses this exact

102
00:06:42,800 --> 00:06:43,040
thing

103
00:06:43,040 --> 00:06:47,870
I'm talking about, talking about the fact that Python is a strongly dynamically

104
00:06:47,870 --> 00:06:52,220
typed language. And I recommend that if you want to dig more into this,

105
00:06:52,370 --> 00:06:56,870
to have a read of this answer and I'll link to this in the course resources.

