forked from trisarasaur/pythongames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessnumber.py
78 lines (60 loc) · 1.87 KB
/
guessnumber.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
# 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
number = 0
guesses = 0
# define event handlers for control panel
def reset():
rand_game = random.randrange(0,2)
print ""
print "NEW GAME!"
if rand_game == 0:
range100()
else:
range1000()
print ""
def range100():
# button that changes range to range [0,100) and restarts
global number, guesses
number = random.randrange(0,100)
guesses = 7
print "The range is 0 - 100"
print "The number of guesses remaining: ", guesses
def range1000():
# button that changes range to range [0,1000) and restarts
global number, guesses
number = random.randrange(0,1000)
guesses = 10
print "The range is 0 - 1000"
print "The number of guesses remaining: ", guesses
def get_input(guess):
global number, guesses
my_guess = int(guess)
guesses -= 1
print "Guess was ", my_guess
print "Number of guesses remaining: ", guesses
if guesses < 0:
print "You lose! The number was ", number
reset()
elif number < my_guess:
print "Guess lower!"
elif number > my_guess:
print "Guess higher!"
else: #number == my_guess:
print "You guessed it! The number is ", number
reset()
print ""
# create frame
frame = simplegui.create_frame("Guess the Number!", 300, 300)
# register event handlers for control elements
frame.add_button("Range is [0-100]", range100, 150)
frame.add_button("Range is [0-1000]", range1000, 150)
frame.add_input("Guess the number", get_input, 100)
# start frame
frame.start()
reset()
# always remember to check your completed program against the grading rubric