-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday12_guessing_name.py
More file actions
61 lines (44 loc) · 1.55 KB
/
day12_guessing_name.py
File metadata and controls
61 lines (44 loc) · 1.55 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from random import randint
EASY_LEVEL_TURNS = 10
HARD_LEVEL_TURNS = 5
def get_turns_by_difficulty():
"""Ask for difficulty and return number of turns."""
while True:
level = input("Choose a difficulty. Type 'easy' or 'hard': ").lower()
if level == "easy":
return EASY_LEVEL_TURNS
if level == "hard":
return HARD_LEVEL_TURNS
print("Invalid choice. Please type 'easy' or 'hard'.")
def compare_guess(user_guess, answer):
"""Return comparison result: 'high', 'low', or 'correct'."""
if user_guess > answer:
return "high"
if user_guess < answer:
return "low"
return "correct"
def game():
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
answer = randint(1, 100)
# print(f"Pssst, the correct answer is {answer}") # For debugging
turns = get_turns_by_difficulty()
while turns > 0:
print(f"You have {turns} attempts remaining to guess the number.")
# Validate numeric input
try:
guess = int(input("Make a guess: "))
except ValueError:
print("Please enter a valid number!")
continue
result = compare_guess(guess, answer)
if result == "correct":
print(f"🎉 You got it! The answer was {answer}.")
return
if result == "high":
print("Too high.")
else:
print("Too low.")
turns -= 1
print(f"😢 You've run out of guesses. The number was {answer}.")
game()