Python tutorial: Guessing a number

Hi everybody.

Welcome to the Python tutorial In this tutorial, I'm going to set up a.

a guessing game to help.

to let you guess a number provided by the computer it is number between 1 and 100 and the computer will guide you to approach the answer by telling you whether your guess is too large or too small then the computer would tell you the number of trials you have used in order to get the correct number and finally the computer would ask you to play the game of not again Let us see the implementation of this game In order to set up a random number, we have to rely on the random package from Python so I have to import it first random is a module provided by Python which can give you a way to generate a random number I will use the functions provided by this module later The user has to at least guess the number once so I can set a variable to remember the intention of the player to do the guessing game I can set the variable play_game to store the intention For example, I can use a small letter y to indicate the intention to play the game Since we don't know the number of times the user is going to play the game, that means when he can guess three numbers or four numbers or even more so I don't know the number of times I have to do the iterations So I can use a while loop to help me do this task If the play_game variable is still equal to y, the computer will loop over again to continue with the guessing game For every set of the game, the computer would generate an answer by means of the random module I use randint() method of the random module It means that the variable answer would be a random integer between 1 and 100 So answer can be one, two, three, four, up to 100 And then I have to set up a variable to store the user input We can call this variable try_number I make a prompt to let the user input a number and then I store the number in try_number variable here OK? try_number is a string when we use the input command here So I have to change it to an integer before I can do the comparison between try_number and answer directly I use int command to change try_number from a string to an integer The game will need at least one iteration to let the user guess the number correctly, so they counter would be at least 1 If the user input is not equal to the answer provided by the computer, I would prompt the user to give a larger or smaller number If the user user input is larger than the computer value, I would prompt the user to provide a smaller number by saying that your number is too large On the other hand, if the user input is smaller than the correct answer, it means that the number provided by the user is too small I will prompt the user to import a larger number by saying that your number is too small After the prompt, I have to save the value provided by the user again So I can reset try_number to be another user input You see, I combined two lines from line 10 and 11 into one line shown on line number 19 here Since the user input is not equal to the a computer value, more trials are needed So the counter would be added by 1 to increment the value stored in the counter variable We will explain the effect of the counter a bit later If the user is able to get the answer correctly, this while loop will no longer be executed Instead, I will simply say that You got it! You tried how many times to get the answer correctly So if the user is able to get the correct answer in just one trial, counter will still be one because this whole while loop is not executed The counter value will still be one OK? If for the first time, the user is not able to get the answer correctly, counter would become one plus one, which is equal to two If the while loop is executed again, and the user is able to get the answer correctly, Then this part will no longer be executed So the counter will still be having a value of 2 OK? So if the user uses only two trials, I will say that you got it, you tried 2 times And after the user has guessed the number correctly, I will prompt the user to play the game again or not So I will say something here The prompt is "continue" And the play_game variable will store the user input if it is equal to y, which is a small letter y the whole game will be executed again If the play_game is no longer small letter y, the game will end that means I will jump out of the whole while loop to the line that is beyond line number 22 It means that the program ends So let us see the effect I guess a number try 60 It is too large so so I have to guess a bit smaller how about 50? still too large 23? Too small So the number is between 23 and 50 And how about 42? Too large So the number should be between 23 and 42 How about 35? It is too large 32? It is too large 30? How about 29? 28? Yes, I got it The guessing number is 28 and we needed one two three four five six seven eight nine 9 times to get the answer correctly I want to continue the game, so I press y Enter So I will play the game again 70? Too large 26? Too large Oh, so the number should be pretty small 13? Too small So the number is between 13 and 26 21? Too large So it should.

May be try 20? Too large 18? Too small So it should be 19 Yes, I got it I tried one two three four five six seven seven times to get the correct answer Continue or not? I don't want to continue, so I press any other letter other than small letter y For example, I press n, smaller n OK, the program ends My implementation is a bit intuitive at straightforward, but it may not be the most efficient version If you can think about any improvement of my code, you can post your code on the comment section of this web page which is below the video If you like this video, please give me a like and don't forget to subscribe to my channel Thank you for watching.

Source: Youtube