-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniProject_Week2.py
More file actions
97 lines (73 loc) · 2.36 KB
/
MiniProject_Week2.py
File metadata and controls
97 lines (73 loc) · 2.36 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
# 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 simplegui
import random
import math
# initialize global variables used in your code
secret_num=0
low_num = 0
high_num = 100
guess_num = 0
max_guesses = 0
# helper function to start and restart the game
def new_game():
# remove this when you add your code
global secret_num, max_guesses
secret_num = random.randrange(low_num, high_num)
max_guesses = int(math.ceil(math.log(high_num - low_num + 1, 2)))
print
print "**** New Game ****"
print "Range is from",low_num,"to",high_num
print "Total guesses :",max_guesses
print
# define event handlers for control panel
def range100():
# button that changes range to range [0,100) and restarts
global low_num, high_num
low_num = 0
high_num = 100
new_game()
def range1000():
# button that changes range to range [0,1000) and restarts
global low_num, high_num
low_num = 0
high_num = 1000
new_game()
def input_guess(guess):
# main game logic goes here
# remove this when you add your code
global guess_num, max_guesses
try:
max_guesses -= 1
print "Number of guesses remaining :",max_guesses
guess_num = int(guess)
except ValueError:
print "invalid num"
if guess_num == secret_num:
print "Correct"
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
print "~~~ You Won ~~~ Congratulations ~~~"
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
print
new_game()
elif secret_num > guess_num:
print "Higher"
else:
print "Lower"
if max_guesses == 0:
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
print "~~~ You Lose ~~~ Try Again ~~~"
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
print
new_game()
# create frame
frame=simplegui.create_frame("Guess the number", 200,200)
# register event handlers for control elements
inp = frame.add_input('Guess Number', input_guess, 100)
butRestart100 = frame.add_button('Range: 0 - 100', range100)
butRestart1000 = frame.add_button('Range: 0 - 1000', range1000)
# call new_game and start frame
new_game()
frame.start()
# always remember to check your completed program against the grading rubric