Welcome back.
How did you deal with the random game?
Were you able to solve it?
Let's give it a try here.
Now I'm just going to use the test pile, actually, let's just rename it just so it's not confusing.
I'm going to rename it to random not a ransom random game by.
All right.
Now in here, I want to be able to, first of all.
Well, let's think about what we need.
So the first thing we need is to.
Generate.
A number one through ten.
Then what we need to do next is to get an input from user, right?
And then we want to do some sort of an input check.
So let's say check that input is a number one through ten and then.
Check if.
If number is the right guess.
Otherwise.
Ask again.
So that seems like a good program that we can write.
So how can we do this?
Well, the first task is to generate a number 1 to 10.
I know that I'm definitely going to want to import the random library from here and specifically there
rent int.
So I can just say from random import rent hint which we've seen.
And here I can generate the numbers.
So let's call it the answer is going to be rent and.
From 1 to 10.
So this is going to be our machine generated random number that we need to try and guess.
Next is we have to get the input from the user.
So how can we do this?
Well, simple, right?
We can get a guess as input from the user and we get the prompt.
So we'll say guess a number one through ten.
All right, so that's looking good.
What about check that input that it's a number 1 to 10.
So we'd have to do a bit of checking here now because we want to keep looping while the user is guessing.
I'm going to do a while true loop that we have to make sure we break out of so we don't have an infinite
loop.
And in the first part of the loop, I want to check what the user inputs.
So I'm going to say if guess.
Well, we know right away that the input is going to give us a string, so we have to make sure we convert
this into an INT first.
So let's do that.
I'm going to say.
If.
Aren't.
Guess.
Is.
Greater.
Then one.
And it.
Guess is less than.
11.
So in our case, let's do zero here because we want to make sure that if it's one, it's still good.
If it's ten, it's still good.
In that case?
Well, we can actually try and check if it's the right guest.
Now.
What happens if this fails?
What if the user enters a string that can't be turned into an int?
For example, if I go to my terminal here and I just turn on the Python interpreter and say int and
then just give it some gibberish, I get a value error invalid literal for int with base ten.
I have no idea what you're trying to do, so we can check for a value error here.
So what I'm going to say.
Is to try.
This bit of code.
And for now, we'll just pass in here.
And if for some reason it doesn't pass and we'll check for value error, then I'm going to print.
Please enter a.
A number.
Awesome.
So we're checking that the input is a number 1 to 10.
So for now, just to make sure that this is working, let's just do this.
Let's break out of this loop or print.
All good in here.
And break out of this loop, considering that right now we're entering a number 1 to 10.
And let's just make this a little bit smaller just so you can see it better.
There you go.
And for here, we'll just do continue because we'll just keep looping until well, until we get the
right answer.
So again, we're importing rendent, generating some random number 1 to 10.
We're getting the guess from the user.
And then we want to check that the input is a number 1 to 10.
So I have a loop that I'm going to keep running and I'm going to ask it is the number is 0 to 11.
If it is, then we're all good.
If for some reason we get a valuer, then we're going to say, Hey, please enter our number.
So let's just test that up.
I'm going to go to my terminal.
Or actually, we can just run this here.
Let's run it.
All right.
Guess a number 1 to 10.
And I get all good.
Like, I think we need a couple of spaces here.
It looks a little weird.
So let's just say GSA number 1 to 10 and get some spaces.
Let's run that again.
There you go.
And I've actually already noticed a bug that we need to fix if, let's say I enter gibberish.
Oh, boy.
Again, please enter our number.
Please enter a number.
And we need to stop this because it's going to get destroyed.
All right, so what happened here?
Well, we didn't break out of this loop.
We continued, and we just kept going.
Wall to.
And we kept getting a value error.
So you have to be careful here.
Remember when we continue.
It goes back to the tribe block or back to the loop, and it's going to try this again.
But we're not asking for the input or a new input.
Again, this is going to fail.
And then another value error happens and then this is going to fail.
Another value error happens.
So we need to put the guess.
Inside here.
Right inside of the tribe lock.
So now if I run this.
All right, let's guess a number.
Let's give gibberish.
I got.
Hey, please enter a number.
All right, we'll enter a number.
We'll.
We'll enter.
How about zero?
Nope.
This doesn't work.
It's going to keep asking me because, well, the number that we entered, it's less than 1 to 10.
But it's a wrong answer.
We could check is wrong.
You know what?
Let's do that.
Let's say else we'll say.
Hey, Bozo.
I said one through ten.
That'll be a fun one.
All right, let's run this.
Let's do zero.
Hey, Bozo.
I said 1 to 10.
All right, let's do five.
Hey.
I guess the number 1 to 10.
Everything's good.
All good.
All right.
So now what I'm going to do is just see if the guess matches whatever the answer right here is.
Now, before I do that, you see here how the pie charm ID gives me a little hint, a yellow line that
says, Hey, this simplified chain comparison can actually be improved.
It's why IDs are so powerful.
And here I can actually simplify this.
I can say if it guess is.
Higher than zero.
And less than 11.
Then do this and look at that.
I just simplify my code.
And the editor gave me a nice hint.
Okay.
Let's go through here.
So instead of saying print, all good.
Let's say if.
This is the case.
We're going to have another conditional and say if the int.
Guess.
Equals to the answer.
We'll print.
Your.
A.
Genius.
And we'll break out of that.
Now notice here how we're doing it.
Guess a couple of times.
Maybe a better way of doing it is to just do an end here so we don't repeat ourself and run the same
function over and over.
And just have a guess like this.
So, again, we're cleaning up our code, making it read more like English.
That's nice.
And now if we run this.
Guess their number, let's say five.
No, that's not right.
Let's do six.
What about seven?
Eight, nine?
This guy's a tough cookie.
One, two, three, four.
Aw, come on.
Is it ten?
Wow.
We are definitely not geniuses because we just, well, tried ten times, but this looks like it's working.
Let's make sure that we're actually working this properly by cheating a little bit.
I'm going to actually print out here.
The answer.
So I see what the answer is before I type it in.
Let's do that actually.
Right here.
If I click Run.
All right.
The numbers for if I type in for.
All right.
Now I'm a genius.
First try.
All right, we're cheating, but it looks like this is working.
That's pretty cool.
Now the last part here is that up until now I've used the input, but I said I wanted to run it in a
terminal and I want to be able to actually well, give it parameters.
Right.
We give it the start parameter and the end parameter of ran int so we can make the game as hard as we
want.
So that's pretty simple.
We have the import system here.
And we simply say cis dot rgv.
The first parameter, remember, the first one is going to be the file name and then the.
Third parameter, and that's going to be the numbers that we're giving it.
So let's use this file now.
Let's copy the code.
And just for this example, because it's simpler, we'll just add it to our sublime text file.
Oh, and looks like we have to do system V here as well.
Make sure we don't forget that.
So let's fix that as well.
Like that.
That's better.
And if we run this not int because remember the parameters that we're going to give it are going to
be turned into strings.
So we just have to make sure that we convert this into an event like this.
Now, if we just copy this.
Run this over here.
Let's run it again.
So I'm pressing up here because it remembers my previous command.
Let's give that a go.
Guess a number one or two.
Let's go with one.
There you go.
I am a genius and our program is not working.
Our guessing game is now working.
Very, very cool.
If you got this good job.
If you didn't, well, you can just take a look at my code and hopefully see where you went wrong.
But good job getting this far.
I'll see you in the next one.
Bye bye.