-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessing_game.py
85 lines (66 loc) · 2.45 KB
/
guessing_game.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
80
81
82
83
84
85
"""
Python Web Development Techdegree
Project 1 - Number Guessing Game
"""
import random
def display_banner():
banner = """--------------------------------------
|Welcome to the Number Guessing Game!|
--------------------------------------
| Press CTRL+C to quit at any time. |
--------------------------------------"""
return "\n".join(banner_line.lstrip() for banner_line in banner.splitlines())
def random_number():
return random.randint(1, 10)
def get_player_guess():
try:
player_guess = int(input("Pick a number between 1 and 10: "))
if player_guess > 10 or player_guess < 1:
raise ValueError
return player_guess
except ValueError:
print("Input error, please enter an integer from 1 to 10")
return get_player_guess()
def check_guess(correct_number, player_guess):
if player_guess > correct_number:
print("It is lower!")
if player_guess < correct_number:
print("It is higher!")
return correct_number == player_guess
def play_again_prompt():
play_again = input("Would you like to play again? [y]es/[n]o: ")
if play_again.lower().startswith("y"):
return True
elif play_again.lower().startswith("n"):
return False
else:
print("I did not understand.")
return play_again_prompt()
def end_game(high_score):
print("\nThe best score you achieved was {}.".format(high_score))
print("Closing the game, see you next time!")
def start_game():
print(display_banner())
attempts = 0
high_score = 0
correct_number = random_number()
try:
while True:
player_guess = get_player_guess()
attempts += 1
if not check_guess(correct_number, player_guess):
continue
print("You got it! It took you {} tries".format(attempts))
if not high_score or attempts < high_score:
high_score = attempts
if not play_again_prompt():
end_game(high_score)
break
print("The HIGHSCORE is {}".format(high_score))
attempts = 0
correct_number = random_number()
except KeyboardInterrupt:
end_game(high_score)
if __name__ == "__main__":
# Kick off the program by calling the start_game function.
start_game()