Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

# Untitled

By: a guest on Oct 27th, 2012  |  syntax: Python  |  size: 1.60 KB  |  views: 1,964  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. # template for "Guess the number" mini-project
2. # input will come from buttons and an input field
3. # all output for the game will be printed in the console
4.
5. import simplegui
6. import random
7. import math
8.
9. # initialize global variables used in your code
10.
11. num_range = 100
12. num_guess = 7
13.
14.
15. # helper function to initialize the game
16.
17. def init ():
18.     global num_range
19.     global num_guess
20.     global secret_num
21.
22.     secret_num = random.randrange(0,101)
23.     num_guess = math.ceil (math.log( num_range +1, 2))
24.     print "New game. Range is from 0 to", num_range
25.     print "Number of remaining guesses is", num_guess
26.     print secret_num
27.     print ""
28.
29.
30. # define event handlers for control panel
31.
32. def range100():
33.     # button that changes range to range [0,100) and restarts
34.     global secret_num
35.     global num_guess
36.     global num_range
37.     num_range = 100
38.     init()
39.
40. def range1000():
41.     # button that changes range to range [0,1000) and restarts
42.     global secret_num
43.     global num_range
44.     num_range = 1000
45.     init()
46.
47. def get_input(guess):
48.     # main game logic goes here
49.     global secret_num
50.     global num_guess
51.     num_guess -= 1
52.     guess = int(guess)
53.
54.     print "Guess was", guess
55.     print "Number of remaining guesses is", num_guess
56.
57.     if num_guess >= 0:
58.         if guess > secret_num:
59.             print "Lower!"
60.         elif guess < secret_num:
61.             print "Higher!"
62.
63.         elif guess == secret_num:
64.             print "Correct!"
65.             print ""
66.             init()
67.     else:
68.        print "You ran out of guesses. The number was", guess
69.        print ""
70.        init()
71.     print ""
clone this paste RAW Paste Data
Top