What's new

Python Pa help po

BrigthWin

Forum Veteran
Elite
Joined
Jul 21, 2020
Posts
1,639
Reaction
1,768
Points
650
Create a simple number guessing game in Python that generates a random number between 1 and 100 and allows the user to guess the number. The game should provide feedback to the user based on how close their guess is to the random number, indicating whether they are "getting hot," "getting warm," or "not near" the target number. The game should continue until the user correctly guesses the number or decides to quit.

Requirements:

  1. The game should randomly generate a target number between 1 and 100 at the start of each game session.
  2. The game should allow the user to enter their guesses as integers. If the input is not a valid integer, the program should prompt the user to enter a valid number.
  3. The program should keep track of the number of attempts made by the user.
  4. The game should provide feedback to the user based on the difference between their guess and the target number:
    • If the guess is exactly correct, display a congratulatory message and the number of attempts.
    • If the guess is within 5 units of the target number, display "Getting hot."
    • If the guess is within 10 units of the target number, display "Getting warm."
    • If the guess is farther than 10 units from the target number, display "You are not near the number."
  5. The user should have the option to quit the game at any time by typing "quit."
  6. The game should continue until the user correctly guesses the target number or decides to quit. If the user quits, display the correct target number and a farewell message.
  7. The game should be written in Python and use loops and conditional statements to handle the game logic.
 
Here's a Python script that implements the number guessing game according to the requirements you specified:

Python:
import random

def number_guessing_game():
    target_number = random.randint(1, 100)
    attempts = 0

    print("Welcome to the Number Guessing Game!")
    print("I have chosen a number between 1 and 100. Can you guess it?")

    while True:
        guess = input("Enter your guess (or type 'quit' to exit): ")
        
        if guess.lower() == 'quit':
            print(f"The correct number was {target_number}. Thanks for playing!")
            return

        try:
            guess = int(guess)
        except ValueError:
            print("Invalid input. Please enter a valid number.")
            continue

        attempts += 1

        if guess == target_number:
            print(f"Congratulations! You guessed the number in {attempts} attempts.")
            return
        elif abs(guess - target_number) <= 5:
            print("Getting hot.")
        elif abs(guess - target_number) <= 10:
            print("Getting warm.")
        else:
            print("You are not near the number.")

number_guessing_game()

You can run this script in any Python environment. The game will generate a random number between 1 and 100 and prompt the user to enter their guess. The program will provide feedback based on the user's guess and continue until the user correctly guesses the number or decides to quit by typing "quit".
 
image.png
 

Attachments

Back
Top