 Okay, welcome everybody to wordle in the terminal in Python with Tokyo edtech. That is me today We're gonna be making a very simple wordle clone and we're gonna be using Python 3.x series been doing things like inputting information slices of strings We're gonna do some loops. We're using something called NC color codes so if you're not familiar with wordle, let's take a look at what that looks like and It's a kind of a popular game. It was very popular. I found this image on Flickr. So thanks to Tatsu Yamashita So Yamashita-san. Thank you very much. This is Creative Commons licensed So I will try to put a link down below, but you can see it here in the video So basically wordle is a word guessing game. We've got one two three four five letters across so each word is five letters and You need you have six guesses. So one two three four five six So in this particular case if you're not familiar with the game, you'll see that shake is the correct word So when we start you have to guess a word. So in this case the person chose paste P-A-S-T-E So you can see that the E is green because the correct word has green in it Or sorry has E in it not green in it and it is in the same location. It's the last letter You'll see here that A is Yellow because it's in the word, but it's in a different location So we have S here is yellow because it is in the word But in the wrong location and then P and T are grayed out because they are not in the word anywhere So what are we doing is we're doing a command line version of this, right? I have the fancy graphics It's gonna be in the terminal. So it keeps the coding pretty simple, especially for beginners. So let's go ahead and get started Oops wrong one Do we go back there? We're gonna go over to genie. I'm using the genie editor. It's free It's open source and cross-platform. I'm personally using Linux But this should work on Mac and Windows as well. So the first thing I want to do is I'm gonna import OS And I like to do this. I just do it OS system Clear just to clear the screen now if you are on Mac and Linux this will work I think on Windows it's CLS, but I don't use Windows very often So we'll stick to clear and if I go ahead and print Wordle So we know what game we're playing And I'm assuming you know a little bit about Python and hopefully this will help you to put things together So I'm gonna go ahead and run this and just test it. You can see we've got Wordle So the first thing we're gonna need is we're gonna need a correct word So we need the the actual word that we're gonna be using So what we'll do is we'll just go ahead and use shake since that is you know here We can use that as an example. So I'm gonna call it correct or I said correct Equals and shake I'm just gonna put it in capital letters and keep it that way And then what we're gonna need from the user is a guess So I'm gonna do something like guess equals input Please guess very original And so what's gonna happen actually what I'm gonna do here is I'm gonna put dot upper And what that'll do is it will capitalize the guess so whatever the user prints or types It will capitalize it. So let's go ahead and just make sure this is working So let's say my guess is shake Actually, I'll put I'll put it lowercase. So shake and notice it comes out as capital That's what we're gonna be doing is we're gonna be comparing the individual letters Okay, so I'm gonna keep this as simple as possible. I'm gonna not use any functions I'm just gonna keep it all in one One part on one main part here So hopefully this will be good for beginners. I actually just did this recently with my students So it is fresh in my mind. So let's go back to our example So the first thing the person entered was paste So you notice how we have one two three four five letters So let's go ahead and just deal with the first letter and this this is just a way of Breaking down a problem and just making sure that you understand how to do certain things So what I'm gonna do is I'm just gonna go ahead and take a look at the first letter only so What I want to do Say let's say so check first So what I need to do is I need to get the first letter of each of those words So I'm gonna call this correct one actually call it correct zero explain why in a second equals correct zero and guess Zero I say, ah Yeah, I don't really like those names, but we'll go with that for now. Guess Zero so what this does if I print correct zero And I print guess zero This will give me the first letter of each of those let's go ahead and just make sure that's working And what was the first letter or over your paste? Okay? Let's remember that So I'm gonna type paste And we have s and p again the upper method here makes my guess uppercase So s and p so What we gotta think about I'm gonna go ahead and just delete that Okay, is what we need to think about is what are the possible Situations here, okay, so if we go back In this case p does not equal s And it's not anywhere else in the word so we print it out as Just blank or black in the background Hey, so What we want to do is to deal with those three possibilities either. It's the same and It's in the same location It's not in the same location, but it's in the word somewhere else Or it's not in the word at all. So we have three different possibilities and we got a code for those So what we do is I'm gonna say If correct zero equals guess zero And I'm gonna say print And this is not what we're gonna do. I'm just trying to explain to you the the process second case is if Let me switch that around so it makes maybe make it a little bit easier if guess zero Equals correct Zero and if you haven't done slices before It starts at zero. So this is the zero with letter. This is the first. This is zero one two Three and four the length is five, but it's zero one two three and four So print In word I'm gonna be deleting all this in a second else print not in word So again, I'm trying to explain kind of the thought process You know for people that are familiar with this. So let's go ahead and run this So we know the correct word is shake Oops, look at that syntax error. I wonder how many of you caught that So let's go ahead and take a look at that L if that was really dumb guess zero in Correct And what this will do this will tell us if the first letter of my guess Is in this word somewhere Doesn't matter where just as long as it's in there Somewhere let's go ahead and try that Okay, so let's go ahead and do shake And you can see correct. So the first letter is correct Okay, let's go ahead and try it again. Let's try a Just do a a a Okay, it's in the word and in the wrong spot. Remember, we're only looking at the a here And let's go ahead and do paste And the p is not in the word So the structure of our if statements here is pretty good. It seems correct But what we need to do is we need to print the colors So How we do that is something called ansi color codes So I have a link here And this is actually a really amazing blog This project is pretty hardcore But what I want to do is I just want these ansi color codes And so he has them nicely listed. I'll put the link down below So what we want we want background green. I'll explain what this is in a second And we want background yellow And then we also are going to need to use this reset now. There's there's some other ones Um, there is no background gray at least not here. So I'm just going to use black and uh, it's black is the default So we don't have to do anything special for that one Okay, so I'm going to go ahead and copy this I'm going to put this into my code up here And it's bg uh green And I need bg yellow And I'm going to need reset So I'm just going to copy those over And let's get our yellow And I'll show you how they work in a second. Okay, so What this lets us do Is to print Colors to the terminal So in the case where it's correct Watch what I do here. I want to use an f string So I'm going to say correct is going to be bg green And then we reset it back to black and white And what I want to print here if it's correct, it doesn't matter which one I print. I'm going to print guess zero I'm just going to go ahead and copy this So I like to be efficient In the case where it's in the word, but in the wrong spot, it's going to be yellow And in the case where it's not in the word We just print it as it is so it's going to be guess zero Let's go ahead and try it and you'll see what I mean So I'm going to do paste Okay, so you notice nothing happened because it's not in the word I'm going to go ahead and do shake Well, it doesn't matter shake So the s is green because it's in the word. It's in the right spot And then we'll do what was the other one That would just do a Let's run that Run it and it'll be a xxx Notice how it's yellow because there is an a in shake, but it's not the first letter So basically we we've we've accounted for all three of the possible My possibilities So we're actually pretty far along in the program right now um The problem is of course now we want to do h a k and e Okay, so what we could do Is to copy this And then change this to one oops Change all of these to one and then change all of them to Two then change all of them to three Um, and this will work if you don't know what a loop is this is what you got to do If you know what a loop is you're in a much better shape So what we're going to do Is we're going to loop through each letter So we use a four loop and we use i for index So remember it starts at zero goes to one goes to two goes to three goes to four So we use in range Starting at zero and notice i'm putting a five there It doesn't actually get the five it stops at five And notice how I tabbed over So let's go ahead and I got to change the zero to an i Because what I want to happen is I want to do zero Then do one then do two then do three if you don't know what a loop is check out my loop video This isn't really the place for it I suppose. Um, so let's go ahead and test this to see what happens Okay, so I'll try paste because that was the first word And you can see here We got p a s t and e looks pretty good to me. So if we go back here p black yellow yellow black green and that is exactly what we got Here now, of course, you know, if you're observant you're probably saying well, that's kind of weird. It's printing down We should be printing across. Let's go ahead and fix that If you want to print across in python What you do is at the end after the quotation mark you had end equals You can either do single quotes. You can do some double quotes doesn't really matter. I prefer double quotes And what that does is it does not print the end line Don't forget to add print Or you just do it like that at the end. So it goes to the next line for the next round Okay, so that is the basic code for what we want to do here. Let's go ahead and test it. Let's do paste And you can see there p a s t and e Basically we're almost done now And so if you recall back to The original thing here you get up to six guesses So let's go ahead and Code that in there so Now we're getting some like loops inside of loops, which is fine Um, what we want to do is we want to guess so I'm gonna say for Um, I'm just gonna do an underscore here in range Uh six and that will give us six guesses She's zero to five notice that that's an underscore because I'm not actually using it It's just like a little placeholder Um, I could have yeah, I could have put like, you know, guess number or something like that number If it makes more sense to you, but I usually use this underscore when I don't really care, you know, what what's going on So let's go ahead and try this So notice I'm putting some code in I'm testing it making sure it's you know what we want it So paste okay, so please guess So amuse shade share All right, so remember that let's try amuse shade Oh, we're pretty close share And the correct word was shake And we got all four now notice how it's still getting asking us to guess Okay, so let's take care of that All right, so I exit that by using command z So check If the guess is correct So I'm going to do if guess equals correct print You know you win And the game is going to be over. What I'm going to do is I'm going to hit exit I'm going to type exit that will end the program. So let's go ahead and test it Now if you're using genie and you see that this is red just hit enter And then now we can run the program again All right, so let's do what was it? Uh paste amuse share Shame shake Okay, and you notice how it ended there Now let's test it where we don't get it at all So blah blah blah now we have to make sure we have at least five letters And I'm going to ignore that and not deal with it And then here's our last guess And notice It didn't tell us okay, so what we should do is outside the loop Outside the for loop We're going to say oops print uh you lose print, you know The correct word was Put correct in there. Yeah, we'll put an f string So let's go ahead and test it. Um I'm pretty sure it's gonna work, but you never know Court nope Worthy nope Okay, none of those And our last guess You lose the correct word was shake Okay, so In 18 minutes with some explanation. We have a fully functioning wordal game, okay, so Yeah, pretty cool a couple things you might want to add To your game just to kind of make it a little bit more exciting is Notice how of course it's always going to be shake. So what we can do Is we can also import random Use the random module And use random Dot choice And I'm gonna put that in parentheses now watch what I do here This is a list again if you don't know what that is that's okay Just get square brackets here and I got round brackets out here or parentheses So shake let's do share Well, let's do all those words panic What was the amuse Uh, what was the other one set on there? Shade share shake amuse And yeah Shade and don't forget to put the quotation marks So what'll happen what this does is every time we run the program It picks one of these words at random. Now it could be shake because one out of five. Let's try Let's do amuse I was close Share oh, I guess shake again. Yeah, it was like you win. Let's try it one more time amuse You win. Okay, so it is choosing notice. I just tested it just to make sure it was choosing random words I'm always gonna make you always gotta make sure so panic share Oops So shade Oh, I guess it's shake again. I guess it really likes the word shake. Okay, so that is that Yeah, 20 minutes. That's good. That's a good amount of time So again, we use the os and random modules os was just to clear the screen We use random so that we can Choose a random word notice the capitalization is kind of important And we use these ANSI color codes to print different colors for the background And again, if you check out that website, you can see all sorts of different options We made one for loop so that we can have six chances so Get six chances to guess Inside that we guess And then we check each letter and there are five letters indexes zero through four And if the guess is correct, we print with a green background color if it's Not in the right spot, but it's in the word We print with a yellow background. Otherwise, we just print it with a black background And notice it's color what you want to print and reset reset puts it back to black and white And then this will make it so that it doesn't print to the next line so you can print across And then if you do get the correct word Print you in exit the game If you make it to six guesses without getting the correct word you lose and print the correct word So 37 lines of code is not too bad. We could have gotten that down to about 30. Okay So, uh, yeah, you know follow subscribe. Whatever whatever you call it and uh, yeah Keep on coding. Take care