Python Number Guessing Game: Code and Walkthrough
This is a simple Python code that generates a random number and asks the user to guess it until they get it right.
import random
secret_number = random.randint(1, 100)
guess = 0
tries = 0
print('I'm thinking of a number between 1 and 100.')
print('You have 5 chances to guess it.')
while guess != secret_number and tries < 5:
guess = int(input('Take a guess: '))
tries += 1
if guess < secret_number:
print('Too low!')
elif guess > secret_number:
print('Too high!')
if guess == secret_number:
print('Congratulations! You guessed my number in', tries, 'tries!')
else:
print('Sorry, you didn't guess my number. It was', secret_number)
Here's how the code works:
- Import random: This line imports the
randommodule, which allows us to generate random numbers. - Generate a secret number:
secret_number = random.randint(1, 100)generates a random integer between 1 and 100 (inclusive) and stores it in thesecret_numbervariable. - Initialize variables:
guess = 0sets the initial guess to 0.tries = 0keeps track of the number of attempts the user has made. - Print instructions: The code prints instructions telling the user the range of the secret number and the number of guesses allowed.
- Guessing loop: The
whileloop runs as long as the user hasn't guessed correctly (guess != secret_number) and they still have guesses left (tries < 5)- Get user input:
guess = int(input('Take a guess: '))prompts the user to enter their guess and converts it to an integer. - Increment tries:
tries += 1adds 1 to thetriescounter. - Check if guess is too low:
if guess < secret_number:prints 'Too low!' if the guess is lower than the secret number. - Check if guess is too high:
elif guess > secret_number:prints 'Too high!' if the guess is higher than the secret number.
- Get user input:
- Display result: Once the loop ends, the code checks if the user guessed the number:
- Correct guess:
if guess == secret_number:prints a congratulatory message with the number of tries. - Incorrect guess:
else:prints a message indicating the user didn't guess correctly and reveals the secret number.
- Correct guess:
Example Gameplay:
I'm thinking of a number between 1 and 100.
You have 5 chances to guess it.
Take a guess: 50
Too high!
Take a guess: 25
Too high!
Take a guess: 10
Too low!
Take a guess: 15
Too low!
Take a guess: 20
Congratulations! You guessed my number in 5 tries!
How to Play:
- Run the code: Save the code as a
.pyfile and run it from your terminal. - Follow the instructions: The game will tell you the number range and how many guesses you have.
- Enter your guesses: Type your guesses and press Enter.
- Keep guessing: The game will provide feedback after each guess, telling you if your guess is too high or too low.
- See the results: At the end of the game, you'll find out if you guessed the number correctly and how many tries it took.
Try modifying the code:
- Change the range of numbers (e.g., 1 to 500).
- Increase or decrease the number of guesses allowed.
- Add features like hints or a countdown timer.
Enjoy playing this fun and educational Python game!
原文地址: https://www.cveoy.top/t/topic/oYeB 著作权归作者所有。请勿转载和采集!