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:

  1. Import random: This line imports the random module, which allows us to generate random numbers.
  2. Generate a secret number: secret_number = random.randint(1, 100) generates a random integer between 1 and 100 (inclusive) and stores it in the secret_number variable.
  3. Initialize variables: guess = 0 sets the initial guess to 0. tries = 0 keeps track of the number of attempts the user has made.
  4. Print instructions: The code prints instructions telling the user the range of the secret number and the number of guesses allowed.
  5. Guessing loop: The while loop 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 += 1 adds 1 to the tries counter.
    • 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.
  6. 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.

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:

  1. Run the code: Save the code as a .py file and run it from your terminal.
  2. Follow the instructions: The game will tell you the number range and how many guesses you have.
  3. Enter your guesses: Type your guesses and press Enter.
  4. Keep guessing: The game will provide feedback after each guess, telling you if your guess is too high or too low.
  5. 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!

Python Number Guessing Game: Code and Walkthrough

原文地址: https://www.cveoy.top/t/topic/oYeB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录