00:02
Okay, good day, ladies and gentlemen.
00:07
Today we are looking live problem number 9 .8 from the great python book.
00:17
And it wishes us to display numbers in a triangular pattern as from figure 9 .25d.
00:28
Now, this problem is pretty much a, i'd say sort of a combination string practice problem along with ticinter practice.
00:42
So it's kind of a combination of both of them.
00:46
And i should say right off the bat that i did this one way already.
00:55
Ready, but i'm going to try doing it a second way.
01:00
I'm going to see if this way works while i'm doing the instruction.
01:03
So i'm sort of creating this live.
01:06
If it doesn't work, i'm going to use the other method, the other way i did it, and i'm just kind of praying this method works.
01:18
Okay, so the basic idea of what we want to display looks like this.
01:26
It will look like this, and i'll just print out some of the numbers for now.
01:32
We'll go something like this.
01:35
Oops.
01:37
One, enter one, two, maybe like this, two, maybe like this, two, three, oops, not three.
01:55
I mean, i'm sorry, i'm not trying to be.
02:00
I'm not trying to be too stupid about this, but this is the basic idea of it.
02:06
Okay.
02:07
And the way i sort of approached it is first off, i treated each of these separate.
02:18
So like this is, this would be row zero or, yeah, row zero, row zero and then we have row one you know like row one here so i created this row counter which which is going to just count the rows and then in each row what you're going to iterate you're going to iterate.
02:55
So for in each row, we're going to try doing is we're going to iterate over the range from 1 to i plus 2.
03:21
Now or sorry row plus two of course when you use range the last number is ignored so so we couldn't use row here because we'd want the last number to be in there and of course oh i'm sorry i did this wrong because my row should start at one not at zero sorry.
03:53
It should start at one, not zero.
03:54
I actually do want it.
03:58
Right.
03:59
So my rows then, my rows would go, my rows would go four row in range one to 12.
04:18
So this is, this is iterating over the rows and then and then what i did is, and then i go for i in, and this, this iterates over the elements in the, obviously the, those are the elements in the string.
04:48
And in particular, then what i'm after is if, if i is equal to, or is, row plus one, what am i going to do? well, i'm going to, in my string, because i'm actually creating a string here, remember, it's what i'm doing because what i want to display on the canvas object will be a string.
05:25
So it would be my str, is of my str plus equals to, and i'm going to just place a new line character in there.
05:49
So i'm creating the string, this entire, this guy here, this is going to be a string with the first character in the string being one, the second character being a new line character, the third character being a one, the fourth character being a two, the fifth character being a new line character, so on and so forth.
06:14
So i'm creating one big string, and then what i'm going to do is i'm going to pin this string onto a canvas object, which is going to be on a t -kinter window.
06:33
So it's going to be all pinned to a canvas object on a tick -kinter window.
06:40
So first, what i'm doing right now, what i'm talking about here is creating this spring here that's going to be pinned on there.
06:49
So, like i'm sorry, i'm sort of doing this.
06:53
Backwards rather than the way i wanted to do it.
06:58
So i'm iterating over the rows, and then each row i'm creating my elements of the string.
07:06
And by putting the new line character in there, what's it going to do when it gets to here? well, it's going to skip to there.
07:15
So that's going to give me sort of the triangle appearance.
07:20
Anyhow, so moving on, so in the case, so like i said, if i is this guy here at the end of the string, well, i'm going to put a new line character there.
07:37
So it will, when it displays, it will jump to here.
07:42
And then, so then else i'm just going to do my str.
07:58
Yeah my str plus equals to i so or i should say str of i so otherwise i'm just going to pin the the i element to it so if we kind of work through what this is actually doing what this let me see right and then what i want to do is probably what i'm probably going to have to do then after that is then add okay yeah this is kind of the idea so so what's going to happen here when this happens so the i'm first going to create the my string and it's going to be of course an empty string and then what the string is going to look like, it's going to go one new line, one, two, new line, and then one, two, three, new line, and so on and so forth.
09:23
Now, that's kind of my hope as to what it will do, and then when it displays, i hope it will give you the right thing.
09:31
I'm not 100 % certain about this because it's a little, it's different than the way i did it before, but i think it should display correctly.
09:42
Okay, so let's jump in to the actual coding.
09:48
Okay, so the first thing i have to do is, of course, i import to kinter, the kinter module.
10:02
And then i got to create the tkinter object, and i'm going to call it window, is the tk.
10:13
And then i'm going to create a canvas object on there.
10:20
Of course, the first thing is always with, i say, i think my width will say, we'll see.
10:33
Say was 200 and my height will be 200.
10:41
Okay.
10:44
And now what i'm start with is an empty string.
10:47
So i start with an empty string.
10:50
I have to initialize the string object.
10:56
And like, okay, and now what i'm going to do is i'm going to create the nested for loop.
11:05
So so now what i'm doing is creating this string.
11:14
So i'm now going to create the string.
11:16
So i start with the row counter.
11:20
So the row counter tells me what numbers will be in the row.
11:24
So four row in range 1 to 12.
11:31
Because remember the last number never when you use range you always have to go one plus what you desire four row in range 112 so now it's for each row so now i go four i in range range it's one to row plus two here.
12:09
Then i go, if i is equal to is row plus one, then i go my str is just the new line character.
12:30
I just put the new line character in there.
12:34
And then i do an else.
12:36
Just go my str plus equals to the string because remember it's it's an int and you can't you can't concatenate a string and a and a integer object so we have to use the string method okay.
13:09
Oh, i know one thing i forgot to do.
13:11
I'm sorry about that.
13:12
I should have done canvas .hack, of course.
13:18
Sorry about that.
13:19
Okay, so now this creates the string here.
13:24
So this creates my string...