Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
- num_range = 100
- num_guess = 7
- # helper function to initialize the game
- def init ():
- global num_range
- global num_guess
- global secret_num
- secret_num = random.randrange(0,101)
- num_guess = math.ceil (math.log( num_range +1, 2))
- print "New game. Range is from 0 to", num_range
- print "Number of remaining guesses is", num_guess
- print secret_num
- print ""
- # define event handlers for control panel
- def range100():
- # button that changes range to range [0,100) and restarts
- global secret_num
- global num_guess
- global num_range
- num_range = 100
- init()
- def range1000():
- # button that changes range to range [0,1000) and restarts
- global secret_num
- global num_range
- num_range = 1000
- init()
- def get_input(guess):
- # main game logic goes here
- global secret_num
- global num_guess
- num_guess -= 1
- guess = int(guess)
- print "Guess was", guess
- print "Number of remaining guesses is", num_guess
- if num_guess >= 0:
- if guess > secret_num:
- print "Lower!"
- elif guess < secret_num:
- print "Higher!"
- elif guess == secret_num:
- print "Correct!"
- print ""
- init()
- else:
- print "You ran out of guesses. The number was", guess
- print ""
- init()
- print ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement