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
- """Guess the Number - Mini Project 2:
- This is a simple program of Guess the Number. The computer randomly
- chooses a number between 0 and 100 (or 0 and 1000) and the user is tasked
- to guess what the number is.
- The number of guesses is defined by the 'difficulty' of the range to guess.
- """
- # Import the necessary modules for program execution
- import random
- from tkinter import *
- root = Tk()
- root.geometry("200x200")
- root.title("Guess the Number")
- frame1 = Frame(root).grid(row = 0, column = 0)
- # initialize global variables used in your code
- computer_number = 0
- guess_range = 100
- number_of_tries = 7
- # game initialization (runs at program startup and new game initialization)
- def init():
- """function init():
- This function runs at program startup and when a new game is
- initialized. The computer guess is re-initialized depending on the
- global variable for guess_range """
- global computer_number
- computer_number = random.randrange(0, guess_range)
- print("New game started! Guess range is from 0 to %s" % (guess_range))
- print("Number of remaining guesses is %s" % (number_of_tries))
- print()
- # define event handlers for control panel
- def range100():
- # button that changes range to range [0,100) and restarts
- """function range100(): Updates the guess range to 100 and changes
- the number of tries to 7 """
- global guess_range, number_of_tries
- guess_range = 100
- number_of_tries = 7
- init()
- def range1000():
- # button that changes range to range [0,1000) and restarts
- """function range1000(): Updates the guess range to 1000 and changes
- the number of tries to 10 """
- global guess_range, number_of_tries
- guess_range = 1000
- number_of_tries = 10
- init()
- def get_input(guess):
- # main game logic goes here
- """ function get_input(string guess):
- This function is the heart of the program; it takes the input from
- the GUI text box and compares it to the random number generated for
- the computer.
- The first thing this function does is check to see if the input is
- a valid integer, if it is the main game logic processes; if not then
- it prints an error and continues the game.
- Entering invalid input does use up a guess """
- global number_of_tries
- guess = entry_box.get()
- entry_box.delete(0, END)
- if guess.isdigit():
- if int(guess) < computer_number:
- number_of_tries -= 1
- print("Your guess was " + (guess))
- print("Number of guesses remaining %d" % (number_of_tries))
- if number_of_tries == 0:
- print("You ran out of guesses, the number was %d" % (computer_number))
- print()
- if guess_range == 100:
- range100()
- else:
- range1000()
- else:
- print("Higher!")
- print
- elif int(guess) > computer_number:
- number_of_tries -= 1
- print("Your guess was " + guess)
- print("Number of guesses remaining %d" % (number_of_tries))
- if number_of_tries == 0:
- print("You ran out of guesses, the number was %d" % (computer_number))
- print
- if guess_range == 100:
- range100()
- else:
- range1000()
- else:
- print("Lower!")
- print
- elif int(guess) == computer_number:
- number_of_tries -= 1
- print("Your guess was " + guess)
- print("Number of guesses remaining %d" % (number_of_tries))
- print("You guessed right!")
- print
- if guess_range == 100:
- range100()
- else:
- range1000()
- else:
- number_of_tries -= 1
- print("Your guess was " + guess)
- print("Number of guesses remaining %d" % (number_of_tries))
- print("Error! '" + guess + "' is Invalid Input")
- if number_of_tries == 0:
- print("You ran out of guesses, the number was %d" % (computer_number))
- print()
- else:
- print("Try again with a valid number")
- print()
- # create frame
- init()
- b_r100 = Button(frame1, text='Range is [0, 100)', command=range100, width=20, pady=2).grid(row=0, column=0)
- b_r1000 = Button(frame1, text='Range is [0, 1000)', command=range1000, width=20, pady=2).grid(row=1, column=0)
- L1 = Label(frame1, text="Enter a guess:").grid(column=0, row=3, padx=2, pady=6)
- entry_box = Entry(frame1, width=12)
- entry_box.grid(row=4, column=0, padx=2, pady=2)
- entry_box.bind('<Return>', get_input)
- ##frame = simplegui.create_frame("Guess the Number!", 200, 200)
- ##
- ##
- ### register event handlers for control elements
- ##frame.add_button("Range is 0 - 100", range100, 200)
- ##frame.add_button("Range is 0 - 1000", range1000, 200)
- ##frame.add_input("Your Guess?", get_input, 200)
- ##
- ### start frame
- ##frame.start()
- mainloop()
- # always remember to check your completed program against the grading rubric
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement