Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code for IIPP Mini Project [Guess the Number]
# Guess the Number by Anirudh Jayaraman
# dated Thu Jun 11 11:54 PM IST
# import all functionalities from the modules mentioned below
from simplegui import *
from math import *
from random import *
# helper function to start and restart the game
def new_game(num_range = 100):
""" Starts the game from scratch """
# initialize global variables used in your code here
global remaining_guesses, computer_num
# fix the number of guesses available to player
remaining_guesses = int(log(num_range)/log(2) + 1)
# computer guess set to a number between 0 and 100
# at start of game or continuing on whatever range
# was last set (manually)
computer_num = randrange(0,num_range)
print "New game. Range is from 0 to " + str(num_range)
print "Number of remaining guesses is " + str(remaining_guesses)
print "You can also fix the range by clicking on a range button..."
print
# define event handlers for control panel
def range100():
""" Button manually setting range between 0 and 100 """
# button that changes the range to [0,100) and starts a new game
global computer_num, remaining_guesses, num_range
# manually set range as 100
# computer guess accordingly set between 0 and 100
num_range = 100
computer_num = randrange(0,(num_range+1))
remaining_guesses = int(log(num_range) / log(2) + 1)
print "Range is from 0 to " + str(num_range)
print "Number of remaining guesses is " + str(remaining_guesses)
def range1000():
""" Button manually setting range between 0 and 1000 """
# button that changes the range to [0,1000) and starts a new game
global computer_num, remaining_guesses, num_range
# manually set range as 100
# computer guess accordingly set between 0 and 100
num_range = 1000
computer_num = randrange(0,(num_range+1))
remaining_guesses = int(log(num_range) / log(2) + 1)
print "Range is from 0 to " + str(num_range)
print "Number of remaining guesses is " + str(remaining_guesses)
def input_guess(guess):
""" Input field and main game logic """
# main game logic goes here
global remaining_guesses, num_range, computer_num
# determine whether num_range variable is global,
# to avoid NameError in case num_range isn't
# manually altered using either button. Set num_range
# globally to 100 in case it hasn't been altered yet
# using either button.
try:
num_range
except NameError:
num_range = 100
else:
pass
# convert string input to float
guessed_number = float(guess)
# decrement remaining guess (for each play)
remaining_guesses += -1
print "Guess was " + guess
print "Number of remaining guesses is " + str(remaining_guesses)
# if-else construct comparing player guess with
# computer generated number
if computer_num > guessed_number:
print "Higher!"
print
elif computer_num < guessed_number:
print "Lower!"
print
else:
print "Correct!"
print
return new_game(num_range)
# if-else construct when all guesses exhaust
# and player has wrong number
if guessed_number != computer_num and remaining_guesses == 0:
print "Sorry, You lose!"
print
return new_game(num_range)
else:
pass
# create frame
frame = create_frame("Guess the number", 200, 200)
# register event handlers for control elements
frame.add_button("Range in 0 to 1000", range1000, 200)
frame.add_button("Range in 0 to 100", range100, 200)
frame.add_input("Enter a guess", input_guess, 200)
# initialize frame
frame.start()
# call new_game
new_game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment