Program: Number Guessing Game
Goal: The player must correctly estimate a randomly generated number between 1 - 100. The application keeps going until the user guesses the right amount, providing feedback on whether the prediction is too high or too low.
Code:
import randomly
# Game launch function
def number_guessing_game():
print("Welcome
to the Number Guessing Game!")
print("I've
chosen a number from 1 to 100. Attempt to guess it.")
# generate a random number between 1 and 100.
number_to_guess = random.randint(1, 100)
# Set the
initial number of attempts
attempts = zero
while True:
# Get the user's guess through guess =
input("Enter your guess:")
# Verify
if the input is a genuine integer; if not, make
a guess.isdigit():
print("Please enter a valid number.")
proceed
Guess is equal to int(guess) … attempts += 1.
# Compare
the guess to the random number.
if guess < number_to_guess: print("Too low! Try again.")
elif guess > number_to_guess:
print("Too high! Try again.")
Otherwise:
"Congratulations!"
print(f) In {attempts} attempts, you have correctly
estimated the number {number_to_guess}.")
pause
# Launch the number_guessing_game ()
Justification:
1. Random Number Generation: Using random.randint(), the application creates a
random number between 1 and 100.
2. User
Input: It asks the user to enter a guess and uses
isdigit() to make
sure the input is a true integer.
3. Feedback: The application indicates whether a guess was too high or too
low after each one.
4. Looping Until Right Guess: The game keeps
going until the player guesses the right number, and it
keeps track of how many times they try.
How to Run:
1. Insert
the code into a number_guessing_game.py
file.
2. Launch
the Python application,
number_guessing_game.py, in your terminal or
command line.
Qualities:
• User-friendly interface: The program gives
clear instructions and feedback for the
user.
• Input validation: confirms that a valid number is entered by the user.
• Replayability: The game is unique each time it is played because the random number is produced every time.
This user-friendly software shows how to work with
conditionals, loops, random number creation, and user input. It's a fantastic addition to any beginner-friendly
Python tutorial website!
0 Comments