-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguesser.py
33 lines (27 loc) · 922 Bytes
/
guesser.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
from random import randint
def is_valid(s):
"""Check whether the input is valid, return boolean."""
if s.isdigit() and 1 <= int(s) <= 100:
return True
else:
return False
number, count = randint(1, 100), 0
print('I made a number from 1 to 100. Can you guess it?\n')
while True:
while True:
guess_str = input("What's your guess?.. ")
print()
if is_valid(guess_str):
guess = int(guess_str)
count += 1
break
else:
print('Invalid input. Please enter an integer in the range from 1 to 100.')
if guess > number:
print('No, my number is less than yours.')
elif guess < number:
print('No, my number is greater than yours.')
else:
print(f'Yes, you are correct! It took you {count} attempts to guess my number!')
break
print('Thanks for playing! Hope to see you again!')