56.2k views
3 votes
5.3.8 higher / lower 2.0 python

User Shaheen G
by
8.1k points

1 Answer

5 votes
Here's an example of a simple higher/lower game in Python:

```
import random

# generate a random number between 1 and 100
number = random.randint(1, 100)

# initialize the guess counter
guesses = 0

# start the game loop
while True:
# ask the user to guess the number
guess = int(input("Guess the number (between 1 and 100): "))

# increment the guess counter
guesses += 1

# check if the guess is correct
if guess == number:
print("Congratulations, you guessed the number in", guesses, "guesses!")
break
# if the guess is too low, tell the user to guess higher
elif guess < number:
print("Higher!")
# if the guess is too high, tell the user to guess lower
else:
print("Lower!")
```

In this game, a random number between 1 and 100 is generated, and the user has to guess the number. After each guess, the program tells the user whether the actual number is higher or lower than the guess. Once the user guesses correctly, the program displays the number of guesses it took to guess the number, and the game ends.
User S Krishna
by
8.1k points

No related questions found