Building a Fun and Interactive Number Guessing Game in Python project 13

Building a Fun and Interactive Number Guessing Game in Python



Introduction:

Have you ever wondered how to create a simple yet engaging game using Python? Look no further than the Number Guessing Game! In this article, we will explore the process of building a Python program that challenges players to guess a randomly generated number. It's a fantastic project for beginners, allowing them to practice their coding skills while having fun.


To begin, we'll import the necessary modules, including the "art" module, which adds a touch of visual appeal to our game. The program will welcome the players and provide instructions, informing them that they need to guess a number between 1 and 100. The random module comes into play here, as it allows us to generate a random number for the players to guess.


To add an extra level of challenge, we introduce difficulty levels. The players can choose between "easy" and "hard." In "easy" mode, they receive 10 attempts to guess the number, while "hard" mode grants them only 5 attempts. This feature adds excitement and encourages players to strategize their guesses.


The core of the game lies in a while loop that continues until the player guesses the correct number or runs out of attempts. Each time the player makes a guess, the program provides feedback, indicating whether the guess is too high or too low. It also updates the player on the number of attempts remaining.


Once the game concludes, a message is displayed to inform the player whether they won or lost. If the player guesses the number correctly, they receive a congratulatory message. However, if they exhaust all their attempts without guessing correctly, they are notified of the correct answer and that they have lost the game.


The Number Guessing Game is an excellent opportunity to dive into the fundamentals of programming with Python. It covers concepts such as importing modules, generating random numbers, user input, conditional statements, loops, and more. By working on this project, beginners can enhance their problem-solving skills and gain confidence in their coding abilities.


So, why not give it a try? Challenge yourself to create an interactive game that entertains and tests your friends' guessing skills. Happy coding!

Replit link: https://replit.com/@safidullah1/Guess-a-Number-interesting-game?v=1

Source code:




from art import logo import random #print logo, and welcome message. print(logo) print("Welcome to the Number Guessing Game!") print("I'm thinking of a number between 1 and 100.") #generate random number and store in random_number. random_number = random.choice(range(1,101)) #choose difficulty . Difficulty = input("Choose a difficulty. Type 'easy' or 'hard': ").lower() Lives =0 if Difficulty == "easy": Lives=10 print ("You have 10 attempts remaining to guess the number. ") else: Lives = 5 print ("You have 5 attempts remaining to guess the number. ") #using while loop to compare the guess number with random number. while Lives >= 1 and Lives !=0: UserGuess = int(input("Make a guess.").lower()) if UserGuess == random_number: print(f"You got it! The answer was {random_number}.") break elif UserGuess > random_number: print(f"Too high.\nGuess again.\nYou have {Lives - 1} attempts remaining to guess the number.") elif UserGuess < random_number: print(f"Too low.\nGuess again.\nYou have {Lives - 1} attempts remaining to guess the number.") Lives -= 1 if UserGuess != random_number: print(f"You've run out of guesses, you lose. Guess number = {random_number}")

#/////////////art.py file

logo = """ ___ __ __ ____ ___ ___ ___ __ __ __ ____ / __)( )( )( ___)/ __)/ __) / __) /__\ ( \/ )( ___) ( (_-. )(__)( )__) \__ \\__ \ ( (_-. /(__)\ ) ( )__) \___/(______)(____)(___/(___/ \___/(__)(__)(_/\/\_)(____) """

Next Post Previous Post