-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuess the number game
More file actions
68 lines (53 loc) · 1.84 KB
/
Guess the number game
File metadata and controls
68 lines (53 loc) · 1.84 KB
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
import random
import simplegui
chance = 7
secret_number = 1
# helper function to start and restart the game
def new_game():
global secret_number
secret_number = random.randint(0, 100)
# define event handlers for control panel
def range100():
"""store range to secret_number"""
global secret_number
secret_number = random.randrange(0, 101)
def range1000():
"""store range to secret_number"""
global secret_number,chance
secret_number = random.randrange(0,1001)
chance = 10
def input_guess(guess):
"""compare guess with secret number"""
global chance,secret_number
try:
guess = int(guess)
chance -= 1
if chance > 0:
print "\nYour guess was", guess
if (guess > secret_number):
print "Lower!"
print "Chances left:",chance
elif(guess < secret_number):
print "Higher!"
print "Chances left:",chance
else:
print "\nCorrect!!!\n"
print "You guessed it right!!!\nCorrect number was %s, you had %s chances left...\nChoose a New game. " %(secret_number, chance)
else :
print "\nTime's up, You Lost\nCorrect number was %s"%(secret_number)
print "Try again a new game."
range100()
except:
print 'Input is not a number!!!\nTry Again!!!'
def end_game():
"""closes game"""
f.stop()
# create frame
f = simplegui.create_frame("Guess the number",250,250)
# register event handlers for control elements and start frame
f.add_button("Range is from 0 - 100",range100 ,150)
f.add_button("Range is from 0 - 1000",range1000 ,150)
f.add_input("Enter a number", input_guess, 100)
f.add_button('End Game', end_game, 70)
# call new_game
f.start()