-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuessANumberGame.py
74 lines (65 loc) · 2.07 KB
/
GuessANumberGame.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import random
def StartGame():
low = 1
high = 10000
personalBest = 100000
print('Welcome!')
while True:
PrintMenu()
selection = input()
if selection == '2':
break
if selection == '1':
personalBest = PlayGame(personalBest, low, high)
if selection == '3':
low = ChangeLow()
high = ChangeHigh(low)
print('Range changed to ' + str(low) + ' to ' + str(high))
personalBest = 100000
print('Bye')
def PlayGame(personalBest, low, high):
print('Guess a number between ' + str(low) + ' and ' + str(high))
number = random.randint(low,high)
tries = 0
while True:
guess = input()
if not guess.isdigit():
continue
tries += 1
if int(guess) > number:
print(guess + ' was too high')
if int(guess) < number:
print(guess + ' was too low')
if int(guess) == number:
print('You are right!')
print('The number was ' + str(number))
print('Took you ' + str(tries) + ' tries to find it')
if tries < personalBest:
print('New Personal best ' + str(tries) +' tries, last best was ' + str(personalBest))
return tries
else:
print('Your personal best is ' +str(personalBest) + ' tries')
return personalBest
def ChangeLow():
newLow=''
while True:
print('Input new low')
newLow = input()
if(newLow.isdigit()):
return int(newLow)
def ChangeHigh(low):
newHigh = ''
while True:
print('Input new high')
newHigh = input()
if newHigh.isdigit():
if int(newHigh) > low:
return int(newHigh)
else:
print('New high must be higher then ' + str(low))
def PrintMenu():
print('Select option')
print('1. Play')
print('2. Quit')
print('3. Change Low and High')
StartGame()