Advertisement
carlosjuero

Coursera - Intro to Interactive Python - TKinter GTN

May 9th, 2013
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.46 KB | None | 0 0
  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. """Guess the Number - Mini Project 2:
  5.    This is a simple program of Guess the Number. The computer randomly
  6.    chooses a number between 0 and 100 (or 0 and 1000) and the user is tasked
  7.    to guess what the number is.
  8.  
  9.    The number of guesses is defined by the 'difficulty' of the range to guess.
  10. """
  11.  
  12. # Import the necessary modules for program execution
  13.  
  14. import random
  15. from tkinter import *
  16.  
  17. root = Tk()
  18. root.geometry("200x200")
  19. root.title("Guess the Number")
  20. frame1 = Frame(root).grid(row = 0, column = 0)
  21.  
  22.  
  23.  
  24. # initialize global variables used in your code
  25. computer_number = 0
  26. guess_range = 100
  27. number_of_tries = 7
  28.  
  29.  
  30. # game initialization (runs at program startup and new game initialization)
  31. def init():
  32.     """function init():
  33.        This function runs at program startup and when a new game is
  34.        initialized. The computer guess is re-initialized depending on the
  35.        global variable for guess_range """
  36.     global computer_number
  37.     computer_number = random.randrange(0, guess_range)
  38.     print("New game started! Guess range is from 0 to %s" % (guess_range))
  39.     print("Number of remaining guesses is %s" % (number_of_tries))
  40.     print()
  41.  
  42. # define event handlers for control panel
  43.  
  44. def range100():
  45.     # button that changes range to range [0,100) and restarts
  46.     """function range100(): Updates the guess range to 100 and changes
  47.       the number of tries to 7 """
  48.     global guess_range, number_of_tries
  49.     guess_range = 100
  50.     number_of_tries = 7
  51.     init()
  52.  
  53. def range1000():
  54.     # button that changes range to range [0,1000) and restarts
  55.     """function range1000(): Updates the guess range to 1000 and changes
  56.       the number of tries to 10 """
  57.     global guess_range, number_of_tries
  58.     guess_range = 1000
  59.     number_of_tries = 10
  60.     init()
  61.  
  62. def get_input(guess):
  63.     # main game logic goes here
  64.     """ function get_input(string guess):
  65.            This function is the heart of the program; it takes the input from
  66.            the GUI text box and compares it to the random number generated for
  67.            the computer.
  68.  
  69.            The first thing this function does is check to see if the input is
  70.            a valid integer, if it is the main game logic processes; if not then
  71.            it prints an error and continues the game.
  72.  
  73.            Entering invalid input does use up a guess """
  74.     global number_of_tries
  75.     guess = entry_box.get()
  76.     entry_box.delete(0, END)
  77.  
  78.     if guess.isdigit():
  79.         if int(guess) < computer_number:
  80.                 number_of_tries -= 1
  81.                 print("Your guess was " + (guess))
  82.                 print("Number of guesses remaining %d" % (number_of_tries))
  83.                 if number_of_tries == 0:
  84.                     print("You ran out of guesses, the number was %d" % (computer_number))
  85.                     print()
  86.                     if guess_range == 100:
  87.                         range100()
  88.                     else:
  89.                         range1000()
  90.                 else:
  91.                     print("Higher!")
  92.                     print
  93.         elif int(guess) > computer_number:
  94.                 number_of_tries -= 1
  95.                 print("Your guess was " + guess)
  96.                 print("Number of guesses remaining %d" % (number_of_tries))
  97.                 if number_of_tries == 0:
  98.                     print("You ran out of guesses, the number was %d" % (computer_number))
  99.                     print
  100.                     if guess_range == 100:
  101.                         range100()
  102.                     else:
  103.                         range1000()
  104.                 else:
  105.                     print("Lower!")
  106.                     print
  107.         elif int(guess) == computer_number:
  108.                 number_of_tries -= 1
  109.                 print("Your guess was " + guess)
  110.                 print("Number of guesses remaining %d" % (number_of_tries))
  111.                 print("You guessed right!")
  112.                 print
  113.                 if guess_range == 100:
  114.                     range100()
  115.                 else:
  116.                     range1000()
  117.     else:
  118.         number_of_tries -= 1
  119.         print("Your guess was " + guess)
  120.         print("Number of guesses remaining %d" % (number_of_tries))
  121.         print("Error! '" + guess + "' is Invalid Input")
  122.         if number_of_tries == 0:
  123.             print("You ran out of guesses, the number was %d" % (computer_number))
  124.             print()
  125.         else:
  126.             print("Try again with a valid number")
  127.             print()
  128.  
  129. # create frame
  130. init()
  131. b_r100 = Button(frame1, text='Range is [0, 100)', command=range100, width=20, pady=2).grid(row=0, column=0)
  132.  
  133. b_r1000 =  Button(frame1, text='Range is [0, 1000)', command=range1000, width=20, pady=2).grid(row=1, column=0)
  134.  
  135. L1 = Label(frame1, text="Enter a guess:").grid(column=0, row=3, padx=2, pady=6)
  136. entry_box = Entry(frame1, width=12)
  137. entry_box.grid(row=4, column=0, padx=2, pady=2)
  138. entry_box.bind('<Return>', get_input)
  139. ##frame = simplegui.create_frame("Guess the Number!", 200, 200)
  140. ##
  141. ##
  142. ### register event handlers for control elements
  143. ##frame.add_button("Range is 0 - 100", range100, 200)
  144. ##frame.add_button("Range is 0 - 1000", range1000, 200)
  145. ##frame.add_input("Your Guess?", get_input, 200)
  146. ##
  147. ### start frame
  148. ##frame.start()
  149. mainloop()
  150. # always remember to check your completed program against the grading rubric
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement