-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber-guess.py
More file actions
228 lines (176 loc) · 5.95 KB
/
number-guess.py
File metadata and controls
228 lines (176 loc) · 5.95 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# mini-project week 3 from Nishit Prajapati
# acmnishit@gmail.com
# in the dialogue box, you have to enter a number
# according to your guess, If that number matches with
# computer gussed number, then you win the game
# you would have 7 maximum try in a game
# you can go on the link below and play the game
'''
https://py2.codeskulptor.org/#user48_Ot18neLUidGWogs.py
'''
###########################################
# input will come from buttons and an input field
# all output for the game will be printed in the console
############################################
import simplegui
# random module is secretely guessing no for computer
import random
# global variable secret_number for computer
# generated random guess
secret_number = 0
# global variable count for counting remaining
# turns for guesses by user
count = 0
# helper function to start and restart the game
def new_game():
return range100()
# define event handlers for control panel
def range100():
# button that changes the range to [0,100) and
# starts a new game
print "New game has started in range [0, 100)"
print "Your remaining guesses are 7."
print "plz enter your guessed number"
print " "
global secret_number
global count
count = 7
secret_number = random.randrange(0, 100)
def range1000():
# button that changes the range to [0,1000) and
# starts a new game
print "New game has started in range[0, 1000)"
print "Your remaning guesses are 10."
print "Plz Enter your guessed number"
print " "
global secret_number
global count
count = 10
secret_number = random.randrange(0, 1000)
def input_guess(guess):
# global variables
global secret_number
global count
# converting input guess string into integer number
input_number = int(guess)
print "Your guess was",input_number
#every time user enters guess, counter is decremented
count -= 1
#variable assignment for less crowdy coding space
a = input_number
b = secret_number
#main decision making starts here
# if according to range, count is within limits,
# then "if" is executed
# no of turns(count) are 7 for "0 to 100" and
# 10 for "0 to 1000"
# if computer guess is between "0 to 100",
# then 1st "if is executed
# if input is also within the same range,
# then decision is made
if (count > 0) and (0 <= b < 100):
if 0 <= a < 100:
if b < a:
print "Lower"
print "Your remaining guesses are",count
print " "
return " "
elif b > a:
print "Higher"
print "Your remaining guesses are",count
print " "
return " "
else:
print "Correct"
print " "
return range100()
else:
print "Guess not in range"
print "Your remaining guesses are",count
print " "
return " "
elif (count == 0) and (0 <= b < 100):
if 0 <= a < 100:
if b < a:
print "Lower"
print "You are out of turns. You lost."
print " "
return range100()
elif b > a:
print "Higher"
print "You are out of turns. You lost."
print " "
return range100()
else:
print "Correct"
print " "
return range100()
#if input is not in range 0 to 100,
# then error message is displayed
else:
print "Guess not in range"
print "You are out of turns. You lost game."
print " "
return range100()
#############################################
# same logic is repeated for 0 to 1000
#############################################
elif (count > 0) and (0 <= b < 1000):
if 0 <= a < 1000:
if b < a:
print "Lower"
print "Your remaining guesses are",count
print " "
return " "
elif b > a:
print "Higher"
print "Your remaining guesses are",count
print " "
return " "
else:
print "Correct"
print " "
return range1000()
else:
print "Guess not in range"
print "Your remaining guesses are",count
print " "
return " "
elif (count == 0) and (0 <= b < 1000):
if 0 <= a < 100:
if b < a:
print "Lower"
print "You are out of turns. You lost."
print " "
return range1000()
elif b > a:
print "Higher"
print "You are out of turns. You lost."
print " "
return range1000()
else:
print "Correct"
print " "
return range1000()
#if input is not in range 0 to 100,
# then error message is displayed
else:
print "Guess not in range"
print "You are out of turns. You lost game."
print " "
return range1000()
# create frame
check = simplegui.create_frame("guess the number", 200, 200)
####register event handlers for control elements and
# start frame
# buttons to play in either "0 t0 100" or
# "0 to 1000" range
check.add_button("Play in range [0, 100)", range100, 100)
check.add_button("Play in range [0, 1000)", range1000, 100)
# input field for user guess
check.add_input("enter your guess here", input_guess, 200)
#frame is started
check.start()
# calls new_game whenever program is run again
new_game()
# always remember to check your completed program against the grading rubric