-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guess number.py
38 lines (32 loc) · 1.12 KB
/
Guess number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import random
# set the difficulty level
difficulty = input("Choose a difficulty level (easy, medium, hard): ")
# set the range of numbers based on the difficulty level
if difficulty == "easy":
min_num, max_num = 1, 10
max_guesses = 6
elif difficulty == "medium":
min_num, max_num = 1, 50
max_guesses = 8
else:
min_num, max_num = 1, 100
max_guesses = 10
# generate a random number within the range
number = random.randint(min_num, max_num)
# initialize the number of guesses
num_guesses = 0
# ask the user to guess the number
while num_guesses < max_guesses:
print(f"You have {max_guesses - num_guesses} guesses left.")
guess = int(input(f"Guess a number between {min_num} and {max_num}: "))
num_guesses += 1
if guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
else:
print(f"Congratulations! You guessed the number in {num_guesses} guesses.")
break
# if the user runs out of guesses, reveal the number
if num_guesses == max_guesses:
print(f"Sorry, you ran out of guesses. The number was {number}.")