User guesses the guest count will get incremented and we’ll have a accurate count of how many times the user has guessed all right the second variable that I want to make is going to be.
And I’m just gonna call it guess limit and this is gonna tell us how many times the user can guess so this will basically determine like how many guesses the user gets so why don’t we set this equal to three I think three guesses is a pretty good limit and finally we want to create one more variable and this is also gonna be an.
Integer and I’m just gonna call this out of guesses and I’m gonna set out of guesses equal to zero and out of guesses will basically tell.
Us whether or not the user has any more guesses and this might not be super clear what this is doing right now but keep following with the video and you’ll see how this comes into play this is.
Actually gonna be really important so we’re actually gonna have to.
Modify our code a little bit so down here we have this set up so the user can keep guessing as long.
As the guess isn’t equal to the secret number but we want to be able to limit the.
Amount of times that the user can guess and actually down here I’m going to create an if statement and every time we go through this loop I want to.
Check and see if the user has any more guesses left I want to in other words I want to see if they’ve reached their guessed limit so I’m gonna make an.
If the guest count is less than the guests limit as long as the guest count is less than the.
Guests limit that means they haven’t guessed as many times as the limit specifies basically they can guess again right so down here inside of this if statement I’m gonna put all of this code and.
I’m just gonna surround this with curly brackets so basically the user can only guess the number this code will only get executed when.
The guest count is less than the guessed limit when the user has additional guesses right when they haven’t guessed as many times as the limit specifies otherwise though so if they have guessed too many times right if the guest count is either equal to or greater than the guessed limit I’m actually gonna set this out of guesses variable equal to one so I’m.
Gonna say out of guesses is equal to.
One and basically what this is going to tell us is if out of guesses is greater than zero then that’ll tell.
Us that the user is out of guesses so essentially if out of guesses has a value that’s more than zero we’ll know that they’ve run out of guesses and if you ever use any other programming languages before this is what we would refer to as like a boolean variable it’s basically storing like a true or a false value which we’re representing.
With zero or one so if the guest counts less than the guessed limit they can guess again otherwise we’re gonna say out of.
Guesses is equal to one in other words we’re gonna say that they’re out of guesses there’s one more thing we have to do though is we have to come up here and we have to change this condition because right.
Now we’re gonna keep looping through this code as long as the guest is not equal to the secret number but there’s actually two situations where we want to break out of the.
Loop the first is when they’ve guessed the number correctly right then we want to come down here and print out you win but we also want to break out of the loop when they’ve run out of guesses so I want to check to see whether.
Or not they’re out of guesses so I’m gonna say guess is not equal to secret number and out of guesses.
Is equal to Z so as long as out of guesses is equal to zero that means that they’re not out of guesses and they still have some guesses left but if the guest counts.
Less than the guessed limit and out of guesses gets set equal to one then out of guesses isn’t going to be equal to zero anymore so we’re.
Gonna break out of the loop so basically there’s two scenarios now that will break us.
Out of the loop either they.
Get the word right or they run out of guesses so one more thing we have to do in this program is come down here and you’ll see down here we’re always printing out you win so no matter what like no matter what happens we’re printing out you win and that’s because before if they made it down here if they made.
It out of that loop that means that they guessed the secret number correctly but there’s actually two scenarios down here where they would have broken out of the loop where there are they’re out of.
Guesses or they guess the secret word so I want to actually check to see which one we’re dealing with so I’m going to check to see if of guesses and I’m gonna check to see if it’s equal to one and.
If it’s equal to one that means that they lost and they ran out of guesses so I’m just gonna say of guesses otherwise though it means that.
They won so I’m gonna say you win and this will basically be like hey you won the game awesome so this is a lot of code I know that and let’s just run this see how we did and we’ll see if our games working properly and then I’ll kind of explain all the code to you guys and actually you know what I just noticed the.
Typo down here I said out of guesses double equals one this is just have to be a single equals so we’re assigning this of value so yeah that was a little typo I had two equals that needs to be one so let’s go over here we’ll run our program and it says enter a number so why don’t we try to lose the game I’m just gonna.
And now we’re on our third and final guess if I don’t get it this time that means we’re gonna lose the game so I’m gonna enter and you’ll see it says out of guesses so we weren’t able to guess it within our three tries and the.
Game told us that we lost let’s run it one more time we’ll try to win so I’ll say like two one now we’re on our third and final guess so if I don’t get it this.
Time we’re going to I’m gonna guess.
Of five and we guessed right so it’s gonna tell us that we want so that’s sort of basically how this little program works and I’m gonna walk you guys through one more time so we have all these variables up here secret.
Number is just storing the number they need to guess the guest keeps track of what they’re guessing each time the guest count is gonna tell us how many times they’ve guessed the number guess.