-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmini project 2 "Guess the number".py
95 lines (70 loc) · 2.22 KB
/
mini project 2 "Guess the number".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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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
num_range = 100
secret_num = 0
guesses_left = 0
# helper function to start and restart the game
def new_game():
global num_range
global secret_num
global guesses_left
secret_num = random.randrange(0, num_range)
if num_range == 100 :
guesses_left = 7
elif num_range == 1000 :
guesses_left = 10
print "New game. The range is from 0 to", num_range, ". Good luck!"
print "Number of remaining guesses is ", guesses_left, "\n"
pass
# define event handlers for control panel
def range100():
global num_range
num_range = 100 # button that changes range to range [0,100) and restarts
new_game()
pass
def range1000():
global num_range
num_range = 1000 # button that changes range to range [0,1000) and restarts
new_game()
pass
def input_guess(guess):
# main game logic goes here
global guesses_left
global secret_num
won = False
print "You guessed: ",guess
guesses_left = guesses_left - 1
print "Number of remaining guesses is ", guesses_left
if int(guess) == secret_num:
won = True
elif int(guess) > secret_num:
result = "Lower!"
else:
result = "Higher!"
if won:
print "That is correct! Congratulations!\n"
new_game()
return
elif guesses_left == 0:
print "Game over. You didn't guess the number in time!"
new_game()
return
else:
print result
pass
# create frame
f = simplegui.create_frame("Game: Guess the number!", 250, 250)
f.set_canvas_background('Green')
# register event handlers for control elements
f.add_button("Range is [0, 100)", range100, 100)
f.add_button("Range is [0, 1000)", range1000, 100)
f.add_input("Enter your guess", input_guess, 100)
# call new_game and start frame
new_game()
f.start()
# always remember to check your completed program against the grading rubric