Building a Guessing Game | Python | Tutorial 21

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I'm going to show you guys how to build a basic guessing game in Python and in this guessing game we're actually going to be using all sorts of cool programming structures that we've learned up to this point in the course so we're gonna be using things like if statements and while loops and variables and all these cool things in order to build this game so the basic idea is we will specify like a secret word so we'll have a secret word that we store inside of our program and then the user can interact with the program and try to guess the secret word and so what we want to be able to happen is we want the user to be able to keep guessing what the secret word is and keep typing in different responses until they get the secret word right so that's the basics of what this game is gonna be and now let's go ahead and create it so the first thing we want to do is create a variable to store our secret word so I could say secret word and we're just gonna set this equal to a secret word so why don't we make it draf that's a pretty good secret word and now what we want to do is we want to create a variable that will store the user's response so I want a variable that will store like all the guesses that the user makes so I'm just gonna call this guess and I'm just gonna set it equal to an empty string right now so now we have the secret word and we have a variable to store the user's guess and what we need to do is we need to be able to prompt the user to input the secret word but here's the catch what we want to happen is we want them to enter the secret word and if they don't guess it correctly we want to prompt them to enter it again so we can't just use a single input statement we actually have to use something called a while loop and we can use a while loop in order to continually ask the person to guess the word until they guess it correctly so let's go ahead and create our while loop so I'm gonna say while and after I say while I need to specify a looping condition or a looping guard basically this is something that as long as it's true we're gonna keep looping through this loop so basically I want to say I want to keep looping as long as the users guess is not equal to the secret word so as long as they haven't guessed the secret word I'm gonna keep going through this loop and inside of this loop what we're gonna do is we're gonna ask them to input the secret word so I can take this guess variable and I can set it equal to input and I'm just gonna say enter guess so over here we're basically telling the user to enter in their guests I'm storing whatever they enter inside of this guess variable and then what's gonna happen is we're gonna come back up here we're gonna check to see if the guess is equal to the secret word if the guess isn't equal to the secret word then we're gonna do it again but if the guess is equal to the secret word then we're just gonna break out of this loop and so we can come down here and we can just print out a success message so I could say you win because they got the secret word so this is a very very simple program but this is essentially all of the code that we need to be able to build a game like this so let's go ahead and play our game we can run it and see how we did so I'm gonna click play and down here you can see it's prompting us to enter a guess so I'm just gonna type in some you know random text we can type in whatever we want and as long as we're not entering that secret word it's gonna keep prompting us to enter different information but if I enter in the secret word so if I enter in a draft now all of a sudden the program is gonna terminate and it's gonna say hey you win because we were able to guess the secret word so that's like a really cool way for us to be able to do this and we actually have a fully functional guessing game but I think this guessing game could actually be improved quite a bit a lot of times when we're making a guessing game we want to set a limit so in other words I want to set a limit for the number of times that the user can try to guess the word so let's say that the user has three tries right they have three guesses in order to guess the word and if they can't guess the word out for three tries then they're gonna lose the game but if they can guess the word inside of three tries then they'll win the game right I think that would be a little bit more of a fun game so why don't we try to program that game basically we're going to set a limit on the number of guesses that the user can have in order to do this we're going to have to create a couple more variables down here in other words we're gonna have to store a couple more pieces of information the first piece of information I want to keep track of is how many times the user has guessed right so we can just make a variable called guess count and we'll just set this equal to zero because initially the user won't have guessed down in this while loop every time we go through the loop I want to increment that guessed count right so every time we've gone through this wildly I want to increment that count because that means the user will have guessed so down here I'm gonna say guess count plus equals one and this is just gonna add one to the guessed Scott actually whoops alright so after each iteration of this loop we're gonna go ahead and add one to the guest count all right so that's the first variable that we're gonna need we're also gonna want to store another variable and this is gonna be called guess limit and basically this is gonna tell us how many times the user can guess the word so I'm gonna it's gonna be guess limit and we'll basically just say three so let's say that the user has 3 tries to guess the word 3 strikes and you're out so the user if they can't get it in three tries then we're gonna basically say that they lose the game and I'm also gonna need one more variable here which we're gonna call out of guesses and I'm just gonna set this equal to false initially so this out of guesses variable is gonna be a boolean and it's gonna tell us whether or not the user is out of guesses so if out of guesses is true that means they have no more guesses right they basically lost the game and if out of guesses is false that means that they still have some guesses left so they can keep playing all right so let's use these different variables in order to make our program more functional so the first thing I want to do is when I go through this loop I want to check to make sure that the user has more guesses right in other words before I let the user enter a guess I want to check to see that they haven't already used up all their guesses so down here I can make an if statement I can say if and inside of the condition I want to check to see that guess count is less than guess limit if guest count is less than guess limit that means that they haven't guessed the total number of guesses that they have so they have some guesses left and if that's the case and if that's true then I'm gonna go ahead and give them a guess and then we'll increment the guest count if this isn't true in other words if they have reached their guessed limit then I'm gonna want to set out of guesses equal to true because they're out of guesses right they have no more guesses because the guest count wasn't less than the guessed limit and so that means that they're out of guesses so they have no more guesses so there's actually one more thing we need to do inside of this while loop we need to add another condition on to this loop guard so right now we're gonna keep looping as long as the guess is not equal to the secret word but remember if the user is out of guesses in other words if they've reached their guessed limit we don't want them to guess any more right so if they ran out of guesses then we want to basically break out of this loop and not give them any more guesses so I'm gonna go ahead and add another condition onto here and I'm gonna say wow they haven't guessed the secret word and they're not out of guesses so it's gonna be not out of guesses then we're gonna keep looping so as long as they haven't guessed the word and as long as they still have some guesses left we're gonna keep looping but otherwise we're gonna break out of the loop and so down here we're printing out you win but actually when we break out of this loop there's gonna be two possible scenarios so again there's two possible ways that this loop could end right either the guess is equal to the secret word so either the user guess the word correctly or the user ran out of guesses and so there's two situations down here that we need to account for and so I'm gonna use an if statement to figure out which is which so I'm gonna say if and we're just gonna type out of guesses and if the user is out of guesses then I want a print of you lose so we're basically gonna be like you lost the game otherwise though if they're not out of guesses that means that they guess the word correctly so we're just gonna print out you win all right so now we have all the logic for this little guessing game set up and let's see if we can run it and play through it so I'm just gonna run this program and now it's gonna tell us to enter a guess so why don't we enter more guesses than we have so we're gonna try to lose the game so I'm just gonna enter one guess two guesses and now I'm on my final guest so if I don't get it here we should actually lose the game and you can see it says out of guesses you lose so we weren't able to guess it in the number of tries that we had let's run the program again and we'll try to win the game so we'll get a couple wrong let's say we're on our last guess and I'm like okay I can do this so I type in the word and BAM we won the game so that's how we can basically create a game where we have a guess limit so this is a lot of code let me walk you guys through this one more time so you can just get a full understanding of what we're doing up here I created a few different variables we created this secret word variable and we created this guest variable then we also created some more variables so we created this guess count variable and this just keeps track of how many times the user has guessed the word and you can see down here every time we give them a guess we're incrementing the guest count we also have guests limit and guests limit is telling the program how many times the user can guess so before I go through this while loop the first thing I'm doing is I'm checking to see that the guest count is less than the guest limit in other words do they still have some guesses left if they do then we're going to get the input from the user otherwise we have this other variable up here called out of guesses and out of guesses is going to tell us whether or not they have some guesses left so if this is equal to false that means they have some guesses if it's equal to true however it means no more dice no more guesses they are done so they lost the game the last thing we needed to do is specify an additional condition up here so we are going to keep looping through the code inside of this loop as long as the conditions up here are true so as long as they haven't guessed the word and as long as they're not out of guesses we are going to keep looping through and so when eventually the user does break out of this loop there's going to be two possible situations the first situation is that they ran out of guesses and so we want to check to see if that's the situation I want to check to see if they're out of guesses if they are we'll print out a lose message if they're not then they must have guessed it correctly so they win and so that is how we can use while loops and if statements and also variables in combination with each other to build a pretty awesome guessing game hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying traffic Academy and you want to help us grow head over to draft Kadim EECOM forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 46,558
Rating: 4.9596329 out of 5
Keywords: Programming
Id: B9ORjeQlPOA
Channel Id: undefined
Length: 13min 2sec (782 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.