-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguess_the_number.py
More file actions
35 lines (35 loc) · 1.08 KB
/
guess_the_number.py
File metadata and controls
35 lines (35 loc) · 1.08 KB
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
import random
EASY_LEVEL_ATTEMPTS=10
HARD_LEVEL_ATTEMPTS=5
def set_difficulty(level_chosen):
if level_chosen == 'easy':
return EASY_LEVEL_ATTEMPTS
else:
return HARD_LEVEL_ATTEMPTS
def check_answer(guessed_number,answer,attempts):
if guessed_number<answer:
print("Your guess is too low")
return attempts-1
elif guessed_number>answer:
print("Your guess is too high")
return attempts-1
else:
print(f"Your guess is right...The answer was{answer}")
return attempts
def game():
print("let me think of a number between 1 to 50.")
answer=random.randint(1,50)
print(answer)
level=input("Choose level of difficulty___Type 'easy' or 'hard':")
attempts=set_difficulty(level)
guessed_number=0
while attempts > 0 and guessed_number != answer:
print(f"You have {attempts} remaining to guess the number.")
guessed_number=int(input("Guess a number:"))
attempts=check_answer(guessed_number,answer,attempts)
if attempts==0:
print("You are out of guesses...You lose!")
return
else:
print("Guess again")
game()