Guess the number is a basic game, you pick a number from a range of numbers, then you ask the user to guess it, the user has a number of attempts to guess the number, if the number the user guessed is incorrect, the game will tell the user if the number the user guessed is lower or higher than the chosen number, repeat until the user guesses the number and wins or runs out of attempts and loses.
Okay, let's first begin by storing the main game options (such as the numbers range, the attempts a user have, etc) into variables.
In this tutorial I will be using 0 as the minNum, 10 as the maxNum, and the user will have 3 attempts.
Next we'll need to pick the random number before the game starts, to do that, we must import a module called "random" so we gain access to random number generators.
Note: it is preferred to put imports at the start of the Python file
As you can see, random is in grey color, it is because we haven't used it anywhere yet. Let's generate a random number and store it into a variable.
We use the random.randrange function to generate a random number, it takes two parameters, the first determines the minimum int number, and the second determines the max int number MINUS one, this is why we added a plus one, otherwise it would only give us from 0 to 9, you can remove the plus one if that's what you're going for.
We have setup everything, now we need to inform the player about the numbers range and code the main loop of the game, we can start with a simple print, a while loop and a variable that contains the user's input
We use fstring here to put the variables we're using into the string.
We'll name the user input variable x, which is equal to minNum minus one because that will never be the correct number, to avoid the user automatically winning incase the number we initiated the variable with was the same as the correct number. The while loop is going to loop forever as of now but we'll fix that soon.
We now have to register the user's input into our variable, we can do that by using input() and converting it to an interger using int()
Now we need to check if our variable is equal to the correct number and break out of the loop and congratulate the player if so.
We add a simple equality if statement, if it is True, the player will get a win message and the loop will exit, now we have to make the incorrect guess condition which informs the player that the number wasn't correct and reduce how many attempts are left.
But we're not done yet! We need to also tell the player if the number is lower or higher than their current guess. We can use another if statement.
We add a simple equality if statement, if it is True, the player will get a win message and the loop will exit, now we have to make the incorrect guess condition which informs the player that the number wasn't correct and reduce how many attempts are left.
But we're not done yet! We need to also tell the player if the number is lower or higher than their current guess. We can use another if statement.
With another if statement we check if the user's guess was higher or lower than the chosen number, we can safely use an else statement here without checking for equality because the parent else statement wouldn't occure if the user's guess was equal to the chosen number.
What's left now is to check how many attempts the user has and inform the user about the attempts count, if the attempts reach zero, the user loses.
With an if statement, we check the attempts count, if it is zero then with an fstring we print the correct number and we break out of the loop. We can simply put the other condition that happens if the user only guessed incorrectly after that if statement, because if the player guesses correctly, the loop exits, and if the player runs out of attempts, the loop also exits before printing the remaining attempts count, so this will only occur when we guess incorrectly and still have some attempts left.
The game should be done. I hope this was easy to follow, if you understood everything, try making this game without looking at this page, if you didn't understand something then try rereading the steps again. Good luck on your game making journey, we all start here!