Your scrolling text goes here
top of page

Making a Python Number Game


Today, we are going to code in a language called Python. It's super easy to learn because it’s easy to read - for the most part, it uses English syntax. We are going to create a number-guessing game where you try to guess the number the computer is thinking of. Sounds cool, right? Let's get into it:


1. To begin, I recommend using the website Replit. This integrated Development Environment is an application that has compiled all of the tools and resources you’ll need to code and test your

Projects.

  • Just go to Replit.com and make an account by using your email. It's that easy.

  • Then, once you create an account, you will see a “Create Repl” blue bar in the top left corner.

  • Click that and click Python and name your project “Numbers game” or “My coding project”.. whatever you like!

  • Click Create Repl.


2. Now that we have our code up, let's import the “random” module, which provides functions for generating random numbers.

  • import random


3. Define the function number_guessing_game() which encapsulates the logic of the game.


Notice I put a hashtag in there. That is called a comment and it tells programmers what would do in their code. It's like little notes you leave for yourself.


4. Then, in the function, set the lower and upper bounds of the number range. It can be whatever you want but for this, we are going to set the lower bound to 1 and the high bound to 100. That means the number can be anywhere between 1 and 100. We are going to set the number of attempts and the player has a maximum of 5 attempts to guess the number.


lower_bound = 1

upper_bound = 100

max_attempts = 5


5. Generate a random number, secret_number, using the random.randint() function,

which selects a random integer within the specified range.


secret_number = random.randint(lower_bound, upper_bound)


6. Print the game instructions, informing the player about the number range and prompting them to make a guess.


print("Welcome to the Number Guessing Game!")

print(f"I'm thinking of a number between {lower_bound} and {upper_bound}. Can you

guess it?")


7. The game loop begins with a for loop that runs for the maximum number of attempts defined earlier:

for attempt in range (1, max _attempts + 1):'

  • sets up the loop, with the attempt representing the current attempt number. The loop will iterate max_attempts" times.


8. Within each iteration of the loop, the player is prompted to enter their guess using the

'input () ' function:


'guess = int(input (f"Attempt (attempt}: Enter your guess: "))'

  • displays a prompt message asking for the player's guess and reads the input as a string. The 'int() ' function is used to convert the input to an integer, since you have to guess a number.

9. The player's guess is compared to the 'secret_number’ using conditional statements. If the guess matches the secret _number, a success message is printed.


if quess == secret number:

print ("Congratulations! You guessed the correct number!")

return


If the guess is lower than the 'secret _number', a "Too low!" message is printed,

prompting the player to try a higher number. If it’s neither too low nor the secret number, the code assumes that it is too high and notifies the user of that.


elif guess < secret_number:

print(“Too low! Try a higher number.”)

else:

print(“Too high! Try a lower number.”)


10. If the player exhausts all attempts without guessing the correct number, a failure message is printed, revealing the secret_number:


print(f"Oops! You ran out of attempts. The secret number was {secret_number}. Better

luck next time!")


11. Finally, outside the function definition, the number_guessing_game() function is called, initiating the game.



CONGRATULATIONS!

Hit “Run” and go ahead and play your game.

I hope you enjoyed it and keep coding!!


 
Author Sriya Siddhamalli is an 18-year-old freshman studying cybersecurity in Arizona. She is a dynamic individual with a love for cooking, music, and the exciting world of technology.

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page