-
Notifications
You must be signed in to change notification settings - Fork 2
/
Guessthenumber.py
32 lines (30 loc) · 988 Bytes
/
Guessthenumber.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
import random
import sys
def gameplay():
'''Guess the number game'''
number = random.randint(1, 1001)
guesses = 0
print("Guess our whole number between 1 and 1000\n")
while True:
try:
player_guess = int(input("Enter your guess here:"))
if player_guess == number:
print("\nCorrect! You win!")
guesses = guesses + 1
break
elif player_guess > number:
print("\nWrong! The answer is lower than that.")
guesses = guesses + 1
elif player_guess < number:
print("\nWrong! The answer is higher than that.")
guesses = guesses + 1
except ValueError:
print("\nThat's not a whole number, try again.")
continue
print('You got it in', guesses, 'guesses!\n')
gameplay()
playagain = str(input("Play again? (yes or no)\n"))
if playagain == 'yes':
gameplay()
else:
sys.exit