-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessNumberPython
92 lines (67 loc) · 2.07 KB
/
GuessNumberPython
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# template for "Guess the number" mini-project
# input will come from buttons and an input field
# all output for the game will be printed in the console
import random
import simplegui
inp_guess = 0
secret_number = 0
count = 0
# helper function to start and restart the game
def new_game():
# initialize global variables used in your code here
global secret_number, count
count = 7
secret_number = random.randrange(0, 100)
print "New Game. Range is from 0 t0 100"
print 'Number of remaining guesses is', count
print " "
def counter():
global count
count = count - 1
if count > 0:
print 'Number of remaining guesses is', count
else:
print "You are Fail!"
print ' '
# define event handlers for control panel
def range100():
# button that changes the range to [0,100) and starts a new game
global secret_number
secret_number = 0
new_game()
def range1000():
# button that changes the range to [0,1000) and starts a new game
global secret_number, count
count = 10
secret_number = 0
secret_number = random.randrange(0, 1000)
print "New Game. Range is from 0 to 1000"
print 'Number of remaining guesses is', count
print ' '
def input_guess(guess):
# main game logic goes here
global input_guess
inp_guess = int(guess)
print "Guess was", inp_guess
counter()
if count == 0:
return new_game()
elif secret_number > inp_guess:
print "Higher!"
print ' '
elif secret_number < inp_guess:
print "Lower!"
print ' '
else:
print "Correct!"
print ' '
return new_game()
# create frame
f = simplegui.create_frame('Guess the Number', 300, 300)
# register event handlers for control elements and start frame
f.add_input('Enter a Guess', input_guess, 100)
f.add_button('Range: 0-100', range100, 100)
f.add_button('Range: 0-1000', range1000, 100)
# call new_game
new_game()
# always remember to check your completed program against the grading rubric