-
Notifications
You must be signed in to change notification settings - Fork 1
/
guessing_game.py
37 lines (30 loc) · 1004 Bytes
/
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
# guessing_game.py
# A simple guess the number game.
import random
start, end = (1, 100)
guesses_allowed = 15
# Generate a number between 'start' and 'end'.
secret_num = random.randint(start, end)
intro = "\n# Hello! I am thinking of a number between {} and {}."
intro += "\n You have {} guesses. GO!"
print(intro.format(start, end, guesses_allowed))
for num_guesses in range(1, guesses_allowed + 1):
# Prompt the user:
try:
guess = int(input("\nTake a guess: "))
except ValueError:
print("* Please submit a valid value...")
continue
# HINT CASES:
if (guess > secret_num):
print("Your guess was too high.")
elif (guess < secret_num):
print("Your guess was too low.")
else:
break # WINNER! --> exit loop
if (guess == secret_num):
print("\nWINNER! You guessed my number in " +
str(num_guesses) + " guesses!")
else:
print("\nSorry, the number I was thinking of was " +
str(secret_num) + ".")