-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_guessing.py
36 lines (29 loc) · 1.04 KB
/
number_guessing.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
# User will try to guess a random number
import random
import sys
def check_if_number(user_input):
if not user_input.isdigit():
sys.exit("Please enter a digit next time. ")
user_input = int(user_input)
if user_input < 0:
sys.exit("Please enter a whole number next time. ")
return int(user_input)
max_range = check_if_number(input("Type a number for the range: "))
random_number = random.randint(0, max_range)
guesses = 0
while True:
user_guess = check_if_number(input("Make a guess: "))
guesses += 1
if user_guess > max_range:
print(f'Please enter a number within the provided range. (Hint: 0 <= Your Guess <= {max_range})')
continue
if user_guess == random_number:
print("You got it! Congratulations")
break
elif user_guess > random_number:
print("You are above the number!")
elif user_guess < random_number:
print("You are below the number")
else:
print("Sorry :( You got it wrong, Please try again")
print(f'You took {guesses} guesses')