-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuessNumber.py
35 lines (28 loc) · 913 Bytes
/
GuessNumber.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
import random
thenumberGuesses=0
def is_valid_num(s):
if s.isdigit() and 1 <= 100:
return True
else:
return False
def main():
number= random.randint(1,100)
guessed_number = False
guess = input("Guess a number between 1 and 100: ")
thenumberGuesses = 0
while not guessed_number:
if not is_valid_num(guess):
if guess == "Q":
break
else:
guess = input ("I won't count that one. A number between 1 and 100 please: ")
continue
if guess < number:
guess = input ("Too low. Guess again: ")
elif guess > number:
guess = input ("Too high. Guess again: ")
else:
print("You got it in", thenumberGuesses,"guesses!")
guessed_number=True
print("Thanks for playing.")
main()