-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_game.py
36 lines (31 loc) · 942 Bytes
/
number_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
import random
#too high/too low messages
#play again option
def game():
#generate random number between 1 and 10 >>> random integer
secret_number = random.randint(1,10)
guesses = []
while len(guesses) < 5:
try:
#get number guess from player
guess = int(input("Guess a number between 1 and 10: "))
except ValueError:
print("{} isn't a number!".format(guess))
else:
#compare guess to secret number
if guess == secret_number:
print("You got it! My number was {}".format(secret_number))
break
elif guess < secret_number:
print("My number is higher than {}".format(guess))
else:
print("My number is lower than {}".format(guess))
guesses.append(guess)
else:
print("You didn't get it! My number was {}".format(secret_number))
play_again = input("Do you want to play again? Y/n")
if play_again.lower() != 'n':
game()
else:
print("Bye!")
game()