0% found this document useful (0 votes)
318 views2 pages

Guess the Number Game Template

This document contains the code for a "Guess the Number" game. It includes functions to start a new game by randomly selecting a secret number within a given range, calculate the maximum number of allowed guesses, and get a user's guess as input. The main logic compares the guess to the secret number, informs the user if their guess is too high, too low, or correct, and keeps track of remaining guesses. The code creates a frame with buttons to select the game range and an input field to get guesses, and calls the new game function to start.

Uploaded by

jcvoscrib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
318 views2 pages

Guess the Number Game Template

This document contains the code for a "Guess the Number" game. It includes functions to start a new game by randomly selecting a secret number within a given range, calculate the maximum number of allowed guesses, and get a user's guess as input. The main logic compares the guess to the secret number, informs the user if their guess is too high, too low, or correct, and keeps track of remaining guesses. The code creates a frame with buttons to select the game range and an input field to get guesses, and calls the new game function to start.

Uploaded by

jcvoscrib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# 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
#my last version
RANGE_LIMIT=100
# helper function to calculate the number of allowed guess
def calculate_guess_max(RANGE_LIMIT):
import math
GUESS_MAX=[Link](RANGE_LIMIT,2)
GUESS_MAX=[Link](GUESS_MAX)
GUESS_MAX=int(GUESS_MAX)
return GUESS_MAX
# helper function to start and restart the game
def new_game():
# initialize global variables used in your code here
import random
global secret_number,RANGE_LIMIT,guess_counter,GUESS_MAX
secret_number=[Link](RANGE_LIMIT)
if RANGE_LIMIT == 100:
GUESS_MAX=calculate_guess_max(RANGE_LIMIT)
guess_counter=GUESS_MAX
#print "GUESS_MAX",GUESS_MAX
print ""
print "Starting new game with range [0,100)"
print "Number of remainig guesses is:",GUESS_MAX
print "Guess a number in the indicated range"
else:
GUESS_MAX=calculate_guess_max(RANGE_LIMIT)
guess_counter=GUESS_MAX
#print "GUESS_MAX",GUESS_MAX
print ""
print "Starting new game with range [0,1000)"
print "Number of remainig guesses is:",GUESS_MAX
print "Guess a number in the indicated range"
#print "secret number is:",secret_number #debugging statement
# define event handlers for control panel
def range100():
# button that changes the range to [0,100) and starts a new game
global RANGE_LIMIT
RANGE_LIMIT=100
new_game()
def range1000():
# button that changes the range to [0,1000) and starts a new game
global RANGE_LIMIT
RANGE_LIMIT=1000
new_game()
def input_guess(guess):
# main game logic goes here
global secret_number,guess_counter
guess=int(guess)
print "Guess was:",guess

if guess < secret_number :


guess_counter -= 1
print "Number of remainig guesses is:",guess_counter
if guess_counter==0:
print "You ran out of guesses. The number was:",secret_number
new_game()
print "Higher"
elif guess > secret_number :
guess_counter -= 1
print "Number of remainig guesses is:",guess_counter
if guess_counter==0:
print "You ran out of guesses. The number was:",secret_number
new_game()
print "Lower"
elif guess == secret_number :
guess_counter -= 1
print "Number of remainig guesses is:",guess_counter
print "Correct"
new_game()
# create frame
import simplegui
frame = simplegui.create_frame('Guess the number',100,180)
inp=frame.add_input('Input guess',input_guess,100)
# register event handlers for control elements and start frame
button1 = frame.add_button('Range is [0,100)',range100,150)
button2 = frame.add_button('Range is [0,1000)',range1000,150)
[Link]()
# call new_game
new_game()
# always remember to check your completed program against the grading rubric

You might also like