Last active
August 29, 2015 14:02
-
-
Save radicalsauce/03f549103ebf3a1900f9 to your computer and use it in GitHub Desktop.
Guess The Number Game (Written for Coursera's An Introduction to Interactive Programming in Python class)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# "Guess the number" mini-project | |
# By Kelly Hale, 3/12/14 | |
# input will come from buttons and an input field in simplegui | |
# all output for the game will be printed in the console | |
import simplegui | |
import random | |
import math | |
# initialize global variables used in your code | |
# random computer guess | |
comp_number = 0 | |
guesses_allowed = 0 | |
game_range = 0 | |
# helper function to start and restart the game | |
def new_game(): | |
if game_range == 1: | |
print " " | |
print "NEW GAME STARTED (Range 0 - 100)" | |
print "You have 7 guesses to get the right number" | |
range100() | |
elif game_range == 2: | |
print " " | |
print "NEW GAME STARTED (Range 0 - 1000)" | |
print "You have 10 guesses to get the right number" | |
range1000() | |
else: | |
print "NEW GAME STARTED (Range 0 - 100)" | |
print "You have 7 guesses to get the right number" | |
range100() | |
# define event handlers for control panel | |
# ONCLICK it tells you you've started a new game and the range | |
def onclickrange100(): | |
print " " | |
print "NEW GAME STARTED (Range 0 - 100)" | |
print "You have 7 guesses to get the right number" | |
range100() | |
# ONCLICK it tells you you've started a new game and the range | |
def onclickrange1000(): | |
print " " | |
print "NEW GAME STARTED (Range 0 - 1000)" | |
print "You have 10 guesses to get the right number" | |
range1000() | |
def range100(): | |
# from button that changes range to range [0,100) and restart | |
# global var declaration | |
global comp_number | |
global guesses_allowed | |
global game_range | |
# assigning ranges and number of guesses allowed | |
comp_number = random.randrange(0,100) | |
guesses_allowed = 7 | |
# game range = 1 is range100() | |
game_range = 1 | |
def range1000(): | |
# button that changes range to range [0,1000) and restarts | |
# global var declaration | |
global comp_number | |
global guesses_allowed | |
global game_range | |
# assigning ranges and number of guesses allowed | |
comp_number = random.randrange(0,1000) | |
guesses_allowed = 10 | |
# game range = 2 is range1000() | |
game_range = 2 | |
def input_guess(guess): | |
# main game logic goes here | |
# global var declaration | |
global comp_number | |
global guesses_allowed | |
global game | |
# converting user input into an int | |
guess = int(guess) | |
# and party time | |
# guess too low | |
if comp_number < guess: | |
print " " | |
print "Your guess was:", guess | |
print "Lower!" | |
guesses_allowed = guesses_allowed - 1 | |
print "You have the following number of guesses left:", guesses_allowed | |
# guess too high | |
elif comp_number > guess: | |
print " " | |
print "Your guess was:", guess | |
print "Higher!" | |
guesses_allowed = guesses_allowed - 1 | |
print "You have the following number of guesses left:", guesses_allowed | |
# guess FTW! | |
else: | |
print " " | |
print "Your guess was:", guess | |
print "Winner!" | |
print " " | |
new_game() | |
# Guesses allowed countdown, if you reach zero, reinitates game | |
if guesses_allowed == 0: | |
print "You lose, the number was", comp_number | |
print " " | |
new_game() | |
# create frame | |
f = simplegui.create_frame("Guess the number", 200, 200) | |
# register event handlers for control elements | |
f.add_button("Range is [0, 100)", onclickrange100, 200) | |
f.add_button("Range is [0, 1000)", onclickrange1000, 200) | |
f.add_input("Enter a guess", input_guess, 200) | |
# call new_game and start frame | |
new_game() | |
f.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment