-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathguess_the_number.py
79 lines (62 loc) · 2.75 KB
/
guess_the_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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from random import randint
logo = """
$$$$$$\ $$$$$$$$\ $$\ $$\ $$\ $$\
$$ __$$\ \__$$ __|$$ | $$$\ $$ | $$ |
$$ / \__|$$\ $$\ $$$$$$\ $$$$$$$\ $$$$$$$\ $$ | $$$$$$$\ $$$$$$\ $$$$\ $$ |$$\ $$\ $$$$$$\$$$$\ $$$$$$$\ $$$$$$\ $$$$$$\
$$ |$$$$\ $$ | $$ |$$ __$$\ $$ _____|$$ _____| $$ | $$ __$$\ $$ __$$\ $$ $$\$$ |$$ | $$ |$$ _$$ _$$\ $$ __$$\ $$ __$$\ $$ __$$\
$$ |\_$$ |$$ | $$ |$$$$$$$$ |\$$$$$$\ \$$$$$$\ $$ | $$ | $$ |$$$$$$$$ | $$ \$$$$ |$$ | $$ |$$ / $$ / $$ |$$ | $$ |$$$$$$$$ |$$ | \__|
$$ | $$ |$$ | $$ |$$ ____| \____$$\ \____$$\ $$ | $$ | $$ |$$ ____| $$ |\$$$ |$$ | $$ |$$ | $$ | $$ |$$ | $$ |$$ ____|$$ |
\$$$$$$ |\$$$$$$ |\$$$$$$$\ $$$$$$$ |$$$$$$$ | $$ | $$ | $$ |\$$$$$$$\ $$ | \$$ |\$$$$$$ |$$ | $$ | $$ |$$$$$$$ |\$$$$$$$\ $$ |
\______/ \______/ \_______|\_______/ \_______/ \__| \__| \__| \_______| \__| \__| \______/ \__| \__| \__|\_______/ \_______|\__|
"""
# add game levels
EASY_lEVEL_TURNS = 10
HARD_LEVEL_TURNS = 5
def check_answer(guess, answer, turns):
"""
Check the user's guess against actual answer. If not right then minus 1 lives.
"""
if guess > answer:
print("Too High")
return turns - 1
elif guess < answer:
print("Too Low")
return turns - 1
else:
print(f" You got it! The answer was {answer} ")
def set_difficulty():
"""
To set game difficulty
"""
level = input("Choose a difficulty. Type 'easy' or 'hard'\n=> ")
if level == "easy":
return EASY_lEVEL_TURNS
else:
return HARD_LEVEL_TURNS
def game():
"""
main game function
"""
print(logo)
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} ")
turns = set_difficulty()
# repeat the guessing if they get it wrong until 0 by difficulty
guess = 0
while guess != answer:
print(f"You have {turns} attempts remaining to guess the number. ")
# let the user guess the number
guess = int(input("Make a guess: "))
# Track the number of turns and reduce by 1 if they get it wrong
turns = check_answer(guess, answer, turns)
if turns == 0:
print("You've run out of guesses, you lose.")
return
elif guess != answer:
print("Guess Again")
else:
print("Game Finish.")
# run the main game function
game()