Untitled

 avatar
unknown
plain_text
2 years ago
1.1 kB
5
Indexable
Certainly! Let's create a simple guessing game in Python where the user has to guess a number within a certain range.

Here's an example:

```python
import random

def guessing_game():
    print("Welcome to the Guessing Game!")
    print("I'm thinking of a number between 1 and 20.")
    
    secret_number = random.randint(1, 20)
    attempts = 0
    
    while True:
        guess = int(input("Take a guess: "))
        attempts += 1
        
        if guess < secret_number:
            print("Too low! Try a higher number.")
        elif guess > secret_number:
            print("Too high! Try a lower number.")
        else:
            print(f"Congratulations! You guessed the number {secret_number} in {attempts} attempts!")
            break

guessing_game()
```

This code generates a random number between 1 and 20 and asks the user to guess the number. It provides hints whether the guess is too high or too low and counts the number of attempts until the correct number is guessed.

You can run this Python code in an environment that supports Python programming, such as an IDE like PyCharm or through an online Python interpreter.
Editor is loading...
Leave a Comment