-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUntitled.rtf
95 lines (94 loc) · 2.78 KB
/
Untitled.rtf
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
{\rtf1\ansi\ansicpg1252\cocoartf1343\cocoasubrtf140
{\fonttbl\f0\fnil\fcharset0 Cambria;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\ri720
\f0\fs24 \cf0 # 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\
\
#global variable secret_number declared\
secret_number = 0\
#global variable range_end declared. Default is 100\
range_end = 100\
#global variable max_turns declared. Default is 7.\
max_turns = 7\
#global variable turns to keep track of the number of turns\
turns = 0\
\
# helper function to start and restart the game\
def new_game():\
# initialize global variables used in your code here\
global secret_number, turns\
secret_number = random.randrange(0,range_end)\
print "\\n\\nStarting new game with range [ 0,",range_end,")"\
turns = max_turns\
print "You have", turns, "turns to guess!\\n"\
\
# define event handlers for control panel\
def range100():\
# button that changes the range to [0,100) and starts a new game\
print "Ending current game..."\
global range_end, max_turns\
max_turns = 7\
range_end = 100\
new_game()\
\
\
def range1000():\
# button that changes the range to [0,1000) and starts a new game\
print "Ending current game..."\
global range_end, max_turns\
max_turns = 10\
range_end = 1000\
new_game()\
\
\
def input_guess(guess):\
# main game logic goes here \
#converting guess to an integer\
global turns\
\
guess = int(guess)\
print "Your guess was", guess\
\
if(guess > secret_number):\
print "You need to guess lower"\
turns -= 1\
print "You have", turns, "turns remaining\\n"\
if (not turns):\
print "Sorry, you have failed. The correct number was", secret_number\
new_game()\
elif(guess < secret_number):\
print "You need to guess higher"\
turns -= 1\
print "You have", turns, "turns remaining\\n"\
if (not turns):\
print "Sorry, you have failed. The correct number was", secret_number\
new_game()\
elif(guess == secret_number):\
print "CONGRATULATIONS! YOU ARE CORRECT!"\
new_game()\
\
\
\
# create frame\
frame = simplegui.create_frame('Guess the number!',200,200)\
\
\
# register event handlers for control elements and start frame\
inp = frame.add_input('Guess?', input_guess, 150)\
r100 = frame.add_button('Range 0 - 100', range100, 150)\
r1000 = frame.add_button('Range 0 - 1000', range1000, 150)\
\
# call new_game \
new_game()\
\
\
# always remember to check your completed program against the grading rubric\
\
}