-
Notifications
You must be signed in to change notification settings - Fork 5
/
guess_the_number.py
73 lines (61 loc) · 2.12 KB
/
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
# 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
# initialize global variables used in your code
secret_number = 0
stop = 100
remaining_guesses = 7
def start_game(stop):
global secret_number, remaining_guesses
if stop == 100:
remaining_guesses = 7
else:
remaining_guesses = 10
secret_number = random.randrange(0, stop)
print "New game! Range is from 0 to " + str(stop)
print "Number of remaining guesses is " + str(remaining_guesses) + "\n"
# define event handlers for control panel
def range100():
# button that changes range to range [0,100) and restarts
global stop, remaining_guesses
stop = 100
remaining_guesses = 7
start_game(stop)
def range1000():
# button that changes range to range [0,1000) and restarts
global stop, remaining_guesses
stop = 1000
remaining_guesses = 10
start_game(stop)
def get_input(guess):
# main game logic goes here
global remaining_guesses
print "Guess was " + guess
remaining_guesses = remaining_guesses - 1
print "Number of remaining guesses is " + str(remaining_guesses)
player_number = int(guess)
if player_number < secret_number:
print "Higher!\n"
if remaining_guesses == 0:
print "You lose. It was " + str(secret_number) + ".\n"
start_game(stop)
elif player_number > secret_number:
print "Lower!\n"
if remaining_guesses == 0:
print "You lose. It was " + str(secret_number) + ".\n"
start_game(stop)
else:
print "Correct!\n"
start_game(stop)
# create frame
frame = simplegui.create_frame("Guess the number!", 200, 200)
# register event handlers for control elements
guess = frame.add_input("Enter your guess", get_input, 50)
frame.add_button("Range is [0, 100)", range100, 200)
frame.add_button("Range is [0, 1000)", range1000, 200)
# start frame
frame.start()
start_game(stop)
# always remember to check your completed program against the grading rubric