Placeholder Image

字幕列表 影片播放

  • What's going on?

  • Everybody welcome to Part 12 of the Python three basics tutorial.

  • Siri's In this video.

  • We're going to learn a bounce, probably one more built in function and talk about how we conduce the the Diagonal victories now to do that.

  • Actually, you know, if you especially with your hard coded there is really only two ways someone could win diagonally so hard coating wise.

  • It's actually pretty easy to cover this one, so long as we're playing a three by three.

  • But I want to maintain the ability for our tick tack toe to be scalable, too, any size, so it's gonna be a little more complex.

  • But again, there's only two different ways in there.

  • Have a pattern to them, so let's go ahead and get into it.

  • So this one was for vertical winners.

  • So all I'm gonna do, I'm just gonna commented out again later.

  • Well, well, well, Adam altogether.

  • But for now I want to work on a diagonal, so let's go ahead and add a diagonal win.

  • Once again, Player, too, is getting more place.

  • It's quite unfair.

  • So So what are the two ways that someone could win?

  • Well, they could either get this spot, this spot, this spot.

  • And what would those be?

  • Well, what would the indexes like, what with the game indexes be like this spot is 00 What's this one?

  • This is 11 What's this?

  • 1 to 2?

  • Very simple, right.

  • Uh, what about this one?

  • This would be to 011 again and zero to notice a pattern.

  • So, at least this way, parole, like the indexes, Are it just identical?

  • You just increments up by land of game, right?

  • Really easy on going the other way.

  • You won.

  • You just go up by Len of game, the other one, You go down starting at, you know, Len of game.

  • Basically.

  • Okay.

  • So if in case that's not making any sense to you guys, you would just say if game 00 double equals and then we're just gonna go Boop and Boop and Boop.

  • Um, and we would have a final check about not equaling zero.

  • Um oh, I'm not worried about that right now.

  • So anyway, uh, winner.

  • And this would this would be 11 And then this would be too, too.

  • So getting 001122 definite pattern there.

  • And then again, this one would be, Let's say it could go either way, but, like 2011 or zero to run that winner.

  • But if we take away that win, no winner, okay?

  • And then what if we give, uh, won the victory in the other direction?

  • Winner.

  • Okay, so pretty cool so we could do that.

  • But again, Yes.

  • I want to be dynamic.

  • I want to keep us, um, not hard coating things because it just it's just gonna bite you later.

  • So So let's go ahead and, um, make something a little more dynamic.

  • So first of all, how would we get, um how could we handle like, this case?

  • So these are two separate cases, and they really are.

  • One is going this way, and one is going this way.

  • So it's handle for the first case here because this one is the easiest.

  • So all we would really need to do is like, let's say we have a list of diagonals because basically the end of the day when we were doing verticals, we build a list and see if all the numbers are the same.

  • We did horizontal.

  • We didn't even have to build a list to see if there were all the same s.

  • So when we do diagonals again, we're just gonna build a list and see if our they all the same.

  • So So that's all we're doing again.

  • So we're gonna say diagonals is a list, and then we're gonna say four and we're say, I x to be index in range of the land of our game.

  • So that will be 012 As we've already seen.

  • All we need to do is diag dot upend and then we append game.

  • Not that whatever that was game and then that i x for index in i X again Really simple, then all we have to do.

  • Well, let's just print, um Diag.

  • I don't think you need to see the this thing, but basically, we're just going to use this logic again, and rather than check, it's going to be diag.

  • Cz Okay, so I'm not gonna run that again.

  • We're just going to see.

  • Is it, um is it the same or not?

  • No, it's not right.

  • 011 But what if we make this one?

  • Sure enough, 111 if we pass it into this little functionality we have here which, by the way, we've already started repeating.

  • Uh, you know what that means?

  • So anyways, um, we can see this.

  • And when we run it through that sort of check logic, uh, it's gonna rain.

  • True.

  • We're gonna know we have a winner.

  • So that way was actually really, really easy.

  • And, um, so now let's do the other direction, because the other direction is going to be a little more challenging, but not much.

  • So let's say we want to go the other way.

  • So I'm gonna I could see what I want to do here.

  • Um, part of me knows we've got so many commented out blocks.

  • You don't have to comment this out.

  • I'm just trying to do it.

  • Just so the only thing you see on screen is the things that we're we're talking about.

  • And by you, I mean me.

  • So, uh, um, so we're gonna go the other way now, So how would we get, uh, you know, like basically one needs to go in order and we've already seen range Len game.

  • That's how we can go in order How can we go in reverse order?

  • Well, we basically just said it.

  • So for, let's just say, um I don't know I in, uh Well, first it was range of Len of game.

  • We can actually reverse that around so we can use another built in function called Reversed.

  • Now, Prince, I Let's just run that real quick to 10 Done.

  • Okay.

  • Pretty cool.

  • And again, as always, if you wanted to learn more about reversed, I mean, there's so much to learn about it.

  • Uh, but if you wanted to, you could check it out.

  • Also, I hopefully by this point, you've realized Oh, my God.

  • There's a lot of really helpful built in functions.

  • And again, you know, if you wanted to do reversed, can you think of a way you would do it yourself or range?

  • I mean, my implementation of range would not be a cz efficient as their implementation or Len or whatever, but, um, if you wanted to do it, you could.

  • And for most of these, I'll see if they're doing it here.

  • Uh, they don't usually someone around here.

  • I don't know.

  • A lot of times you'll see the actual code, Um, that they used to actually produce the thing, but I don't see here anyways.

  • There's been too much.

  • I'm hunting for it.

  • It's somewhere there.

  • Or you could just read the source.

  • Um, anyway, moving along.

  • Also, some of these are like, see implementations.

  • So I'm trying to think of some of them like, um, like pickle pickle and python 2.7.

  • Um, is just like a regular python IQ implementation of pickle.

  • And if you don't know what pickle is ah, we'll talk about it later.

  • But it's just a way for you to, like save your AJ like is it allows you to serialize your object in like basically you can think of.

  • It is it's a way to save an object like a file on your computer and load it back.

  • So it's like already a python object when you load it in.

  • Um, so in like Python, too, that was a pipe your python implementation if I'm not mistaken, and then there's a C pickle in Python to whereas in Python three, it's just the sea implementation.

  • I think they figured like if someone is aware of the existence of sea pickle, they're always going to use he pickles.

  • So why would we do this?

  • So now there are sea implementations.

  • I don't think reversed is one of them.

  • But anyway, maybe it is.

  • Okay, so let us continue along.

  • So now we've got reversed range, Len.

  • Games.

  • So now what if we did the following?

  • Like, what if we said so?

  • We just iterated over.

  • Reversed.

  • What if we, um What if we did something like this?

  • What if he calls equals, um, reversed the first?

  • Why my typing this out even?

  • Just copy, paste.

  • Boom.

  • Okay, so say that's columns.

  • And then Rose equals range.

  • Land game.

  • So now we've got these two.

  • They're not lists, but to minerals.

  • One go 01 to the other one goes to 10 Um, Then what we could do is like, we could, um four.

  • I don't know.

  • I d x in rose.

  • We could, uh, Prince I d x and then calls i d X.

  • Right.

  • That makes sense, I think.

  • No.

  • Oh, well, we pride hit another issue as well.

  • Hold on.

  • You okay?

  • Yeah.

  • So Okay, so we made our first mistake.

  • Um, so let me bring that.

  • Bring that back up, shall we?

  • So let's say you hit this error and you're like What?

  • I don't know what that means.

  • Um, I'm just gonna quit python.

  • Dang it.

  • Um, like, how are you supposed to know what this means, right?

  • That's not fair, is it?

  • Well, let's go back to the professionals.

  • Um, professional coders tool.

  • Okay, I'm just searching literally the error.

  • Let's go to the first result.

  • That says python three.

  • So that's us, anyways, who may be too much.

  • Okay, I can't blah blah blah.

  • Who cares what he's talking about?

  • But he gets this air.

  • The same errors we got scrolling on down, okay, Reversed function returns and it aerator not a sequence.

  • Do you know what that means?

  • Probably not.

  • That's not very helpful to you.

  • He's not wrong, but he's probably not helpful.

  • So then we come back to here and we see the second best answer.

  • I'm noticing a trend at like, the second answer is the most useful for for a beginner.

  • Um, anyway, we can to see very clearly, you have to change it back to a list type.

  • And then the first answer might start to make sense.

  • It's not a list type.

  • So that's what we have to do.

  • We've got to convert it to a list.

  • Okay, so we come back over here, um, we get rid of this error, and we go list Boone.

  • Kind of weird.

  • That range isn't angry.

  • I bet we I mean, we're not trying to call an index on a range, though, because I don't think that would work.

  • That shouldn't work.

  • I actually don't know the answer.

  • And I really kind of want to find out.

  • Let's find out the answer.

  • Can we call an index?

  • So X range three.

  • What if we say ex too, huh?

  • That's pretty cool, I guess.

  • Pry it.

  • Just like next next to the index or something.

  • I'm not really sure.

  • Anyways, that works.

  • You just can't do it on a reverse range or reversed anything, I guess.

  • Okay, whatever, uh, moving along.

  • Okay, so now that we fix that, everything works, and these are the reversed index is that we need, So we're feeling, you know, pretty good, but before remember, like, this isn't really the easiest thing for us to read.

  • Um, and we definitely we do know about, like, index calls index.

  • When someone's reading this, they pry have no freaking clue what you're talking about.

  • The other thing we could say we could use a numerator, for example, and do kind of this very similar task that we're doing.

  • But in this case, we're actually we're not trying to, uh, we're not trying to enumerate.

  • We're not just trying because, like, in this case, we're just conveniently using I.

  • D.

  • X because it happens to be actually an index right, and so conveniently we are able to use it.

  • But that's not actually what we're thinking we're trying to do.

  • We're trying to combine these two things and then it arrayed over them.

  • And that's actually a really common task in programming.

  • And as you might suspect, there's a built in function for it.

  • It is called zip.

  • So what this is going to do is just gonna aggregate elements from each notice that each of the honorables so you could continue passing multiple intervals.

  • So let's let me just quickly, um, why equals range?

  • Uh, I don't want to do that.

  • Uh, let's just a 210 zip x y Okay, so that just creates a zip object So how about four?

  • I ends it, Tex.

  • Why, uh, print, I Okay.

  • You can see they've been zipped together.

  • What if we did?

  • Z equals 567 Uh, and then we just do a sequel.

  • Zip X y z for I in a I think I did one too many spaces there.

  • Print a, uh, four in a mention.

  • Kind of surprised.

  • Why did it say zip object there?

  • I thought we would get away with that four eye in zip X Y z friends I There we go.

  • So for a equals zip for I in a I'm kind of curious wide and that work I must be missing something obvious.

  • Someone calm it down below.

  • What the heck I'm missing.

  • Anyway, My point of this is to show you that you can It's it's an infinite number.

  • It doesn't have to just be too.

  • But anyways, that's really what we're looking to do.

  • And it's gonna make our code ah, lot more readable.

  • So instead, what we we could say, is for, uh, Cole row in zip calls, Rose, rent, call, Rose.

  • Boom.

  • Okay, same outcome.

  • Also, I suspect we could get away with not needing to convert that to a list anymore.

  • Sure enough.

  • Okay, so that looks a lot better also.

  • Okay, so I'm gonna remove two lines here.

  • You don't have to do this.

  • And But sometimes it can be fun to try to condense code.

  • It's more of like a challenge to the programmer.

  • I wouldn't recommend this, but you could cut this paste and cut this pace to pry.

  • At some point, you you're going to exceed the pep A 78 characters per line.

  • Also, what happened here?

  • Why is that?

  • What happened there?

  • I'm gonna do that again.

  • I think we just caught the tail end of rose maybe or something.

  • Is that what happened?

  • Cut paste.

  • OK, okay.

  • And again so we could do the same thing.

  • And then we've eliminated 2 to 2 lines of code, so we could do that.

  • But if if you're quickly reading through this code, it may not be the most clear thing you've ever read in your life.

  • But what we could do to even further condense this down is instead so we definitely need the reversed bit.

  • Um, but what if we did the following?

  • We could also do four coal row in Anouma rate and we just need to enumerate over reversed range.

  • Len Game.

  • And then we could print call, bro.

  • So not hugely shorter than the above.

  • But we're getting there.

  • And, um, a lot simpler than the original code that we wrote.

  • And again we still, uh, end up with the same same results.

  • Now, at least for me.

  • I understand what enumerates doing immediately when I see it reversed.

  • That's pretty clear.

  • I know what reversed is doing, and then we're ranging over the length of the game.

  • I actually find this code to be pretty simple, to let quickly look at and understand what's going on.

  • If you don't, you really could define calls and rose first, but also just the fact that it's four column in row in enumerated tells me.

  • Okay, the indexes, the column in the road must be the reverse range, Len, so that it just I think that makes sense.

  • But you can go overboard for sure.

  • And if you're working in teams or something like that, uh, your teammate may or may not appreciate what you've done.

  • Okay, so then this one would be die.

  • AGS equals this and then you would die.

  • Ags dot up end and then basically game, uh, Cole and then wrote So in in fact would actually wrote call.

  • Let's be Let's be correct.

  • So as we generate over game, row, row, row and then it's called.

  • So it's actually row.

  • Then call them.

  • The other code would have worked just fine, but it would have been wrong.

  • Okay, so, uh diag.

  • Now, let's Prince die, Ags.

  • Okay, 212 Right?

  • And that's true.

  • Now we could go and verse Now it is 2 to 2.

  • We run the check and it will come back true.

  • So this was the other diag.

  • So I'm just gonna uncommon this now.

  • Okay, so this is like the 22 bits of code for the diagonal aspect.

  • I have no idea how long this tutorial has run.

  • Some people have been complaining that the editorials were, like, two short normally, like my tutorials or somewhere in the 10 to 20 minute mark.

  • But people are always complaining there too long.

  • And then the second I make a nine minute video gods too short.

  • So I think like the sweet spot must be like exactly 10 minutes or something aren't of people who've been trained because now creators try to make 10 minute videos because if you make 10 minute or longer video, you can you can throw in a second set of ads.

  • Um, I've never done that, but you can do that.

  • And that's why a lot of creators have started making 10 minutes or 10 minute in one second videos.

  • So anyways, um, honestly, I just go until the topic is covered.

  • So if you think something's too long or too short Sorry, I really don't pay attention to the time.

  • So Okay, so we have done the two diagonals.

  • I'm pretty confident that we've got it, But in the next part, part number 13 What I'm gonna do is we're gonna put everything together, and then we can kind of play with the game map, make sure we'll check everything, Um, and then we should be able to do that within a couple of minutes, and then we'll add user input so the user can actually, like play the game.

  • Now we'll check that, and the code will basically be done.

  • The only other thing I want to make thegame dynamic itself Also, we need to make dynamic string that gets printed right above the game board because that's gonna bother me if that's like the one thing that's not Dynamic, and I think that's about it.

  • Um, and to make that dynamic will use 1/3 party package.

  • Someone did mention it before, but I was like, Go, we're not there yet.

  • Uh, and then, um and then maybe we might use another third party packages to make things look a little prettier.

  • But other than that, we're pretty much done with the TIC tac toe game and the basics.

  • Ah, the only other thing I'm planning to do is that we need I wanna have one more 3rd 1/3 party package that we get into so I can show you guys how you should go about using third party packages.

  • That's what pythons all about.

  • So I'm leaning more so towards Web development, something with flask, just because last pretty easy to quickly do examples with, I might where I could do like data analysis or something like that.

  • So if you have a vote between data analysis or Web development, it's not gonna be a full Siri's.

  • It's just like a quick example, like it would be like 123 videos at Max.

  • Um, let me know down below.

  • If you got some other thing that you'd want to request, you can request that, too.

  • It's gotta be basic enough to like I said, be like 123 videos, But some way to show using flick third party package is basically in working with modules.

  • Stuff like that.

  • So anyways, that's all for now.

  • Questions, comments, concerns, suggestions, whatever.

  • Feel free to leave them below.

  • Otherwise, I'm gonna see you guys in the next video.

What's going on?

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

對角線贏家算法-Python 3編程教程第12頁 (Diagonal Winning Algo - Python 3 Programming Tutorial p.12)

  • 3 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字