so in this video we'll look at how to create python algorithm for simple number guessing
game so this exercise will give you a good understanding of continuous while loop and effect
statement in python programming so let's get started so here we have our python editor so first
what we have to do we have to set the the initial number so i will say initial number equal to 5
and what we have to do is we have to take the attempt so i say number of tries equal to five
and in the next day font will i will print some welcome message so i say welcome to
num number casing
now what we'll do we'll take one while loop so which will continuous look for the input from the
user so i say true now in the next step i will take one try except block
so now in the try block what we're gonna do we're gonna take some number from user so i will say
lucky number equal to input
so we'll take the number from user so he says
please enter your
lucky number from one
now what i have to do i have to probably have to convert into integer so integer
so what happened like let's assume user have entered some strings in that case if i try to
convert into integer it will give me exceptions so that's why we have it this try except block so if
exception occurred then in that case i will print some message
and in the next step i will say continue so now let's go to our actual processing block where
now in the next step what we're gonna do is we're gonna check the
number means these are the invalid numbers so i will print the message and see
and the next step i say continue so in the next step what we have to do
we have to check the number which is enter by user so i check against the lucky number
this number is matching with the input number so i say print and i say u1 i have to break this if not
same number so in that case we'll have it as block first what we do we print the message
and in the next step what we have to do we have to increment this number of try
same time we have to check the this try
so if it is greater than uh greater than 3 then we have to break this loop
then what i say print and in the print message i say
and the next message
and i have to break our loop and let's test this so the our program file name is number underscore
pi so it is asking me the number so let's enter three so it says sorry it's the wrong number let's
enter five so it says u1 so again let's run this i say 4 it says again for wrong number let's say 6
let's say 8 so it says wrong number say 9 i think our loop is not breaking so probably we have to
check the what is happening with the number of tries so here what we have this the number of try
okay hold on here we have to set equal to zero
so now let's run this again say six then four then seven see your three atom accident better luck
next time so that way it will come out after these three attempts or try so now we'll add one more
functionality where we'll ask the user whether do you want to continue playing the game or not
so for that what we'll do we'll take again take the number from the user so i say
again and we'll take the number so i say input
so in the next step i will check that option so say
if again
then save print
then in the print we say thank you for
playing with us
and we break this loop and if it's a user enters y so it comes in the else so i say else
so we assume that user will enter y or n so the so in that case it will comes in else
and we have to set our number so what we i said the number and for that what we'll
use that we'll use the random number so i will say random and then say
random integer and then i will give the range i will say 1 to 10
so for that what we have to do we have to import that module so i say import say random
now where is okay this is our number resetting and now again i have to check the uh reset this
number try as well so i say number try equal to zero and what we say continue
i think this should work so let's save this and again let's test that so again i
run this program and what it says okay something wrong with this program so i think here we have
double equal we need double equal and save it and again run this program so it is asking
the number i say four then again i say five so you it says you won so do you want to continue
i say yes so now it says please enter your number so i enter six and enter four i enter eight
then it says you're at a three attempt accelerate better luck next time
yeah that's all for this video i hope you got a good understanding
how this uh the number game we can implement in the python and thank you for watching this video