PYTHON:
Write a GUI class Game that implements a number guessing game. The GUI should start by choosing a random number between 1 and 100 (using a function from the random module) and then opening up a GUI which allows the user to enter number guesses:
The GUI has an Entry widget for the user to type the number guess and a Button widget labeled “Enter” to enter the guess. The user should be able to enter guesses until he/she makes the correct guess. The GUI should keep track of the number of guesses it takes the user to reach the correct value. Your GUI should be user friendly and erase the guess each time the user presses the Enter button. This means that the user will not need to erase the old guess in order to enter a new one. Each time the user makes a guess, the GUI should indicate whether the user needs to guess higher or lower using a separate window. For example, the window showing that a guess of 50 was too high can be seen below:
If the guess is correct, a separate window should inform the user that she/he has guessed the number and display the number of guesses it took to get there. The following shows that window:
Further, the GUI should handle the cases where the user doesn’t enter a number using exception handling. Invalid values shouldn’t count toward the total number of guesses. Only valid numerical values should be counted. For example, suppose the user enters the following information:
When the user presses enter, the following window should be displayed:
After the user dismisses either the invalid number window or the incorrect guess window, the GUI should erase the text typed by the user and allow the user to enter a guess again:
Once the user has correctly guessed the value, the GUI should choose a new random value and allow the user to restart the game. This should be done by calling the new_game() method in the appropriate place in the event handler. You should start with the template provided in the template file. It shows the organization of the Game class, including the methods you are expected to write. The constructor has been implemented for you and shouldn’t be changed unless you want to use grid instead of pack as your geometry manager. You should test your class by writing: Game().mainloop().
2. Develop a GUI that can be used to teach elementary school children addition and subtraction. The GUI contains two Entry widgets and a Button widget. At start up, the program will generate two single-digit pseudorandom numbers a and b and an operation op, which should be either addition or subtraction. Addition and subtraction should be equally likely to be chosen by your GUI, and the choice of the numbers should be independent of the choice of the operation. Do not in any way link the choice of the numbers and the operation. The functions found in the random module are helpful here. The expression a op b will be displayed in the first Entry widget, unless a is less than b and the operation is subtraction in which case b op a will be displayed. (We don’t want the children to be confused by negative answers). Examples of expressions displayed could be 3 + 2 =, 4 + 7 =, 5 – 2 =, 3 – 3 =, etc. An expression like 2 – 6 = would not be displayed. The child will enter, in the second Entry widget, the result of evaluating the expression shown in the first Entry widget and click the Enter button. If the correct result is entered, a window should pop up indicating that the child got the answer
correct. The following shows a sample entry when you write Ed().mainloop() in the IDLE window:
Suppose the child types an incorrect answer into the rightmost Entry box:
When the child clicks the Enter button a message will pop up:
After that message box is dismissed the incorrect answer is cleared:
Once the child types the correct answer into the Entry:
And clicks Enter, then the pop up window with a positive message is displayed:
When this happens, the GUI should generate a new problem and display it for the child so that he/she can continue. This should continue indefinitely. An example is shown below:
Your GUI should recover from non-numeric entries. When the child types something that cannot be evaluated to a number, such as the following:
then the following pop-up window should display:
After this box is dismissed the invalid value should be cleared so that the child can try again. The problem should not be changed until the child has gotten the answer correct.
The constructor and make_widgets() method have been written for you and can be found in the template file uploaded to D2L. Do not change these methods. Instead you must write the new_problem() and evaluate() methods. The new_problem method generates a new arithmetic problem according to the rules described above and places the problem into the relevant entry. The evaluate() method is the handler for the button. It evaluates the child’s answer entered into the second entry against the answer for the problem found in the first entry and takes the appropriate action based on the situation.
Expert Answer
# Demonstrates text and entry widgets, and the grid layout manager.
from Tkinter import *
import random
number = random.randrange (100) + 1
tries = 0
class Application(Frame):
“”” GUI application which can retrieve an auto number to guess. “””
def __init__(self, master):
“”” Initialize the frame. “””
Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
“”” Create button, text, and entry widgets. “””
“”” Instruction Label “””
# Create instruction label for Program
self.inst_lbl = Label(self, text = “Follow the Steps”)
self.inst_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W)
“”” Player Name “””
# Create label for name
self.name_lbl = Label(self, text = “Player Name: “)
self.name_lbl.grid(row = 1, column = 0, sticky = W)
# Create entry widget to accept name
self.name_ent = Entry(self)
self.name_ent.grid(row = 1, column = 1, sticky = W)
“”” Guess Input “””
# Create label for entering Guess
self.guess_lbl = Label(self, text = “Enter your Guess.”)
self.guess_lbl.grid(row = 2, column = 0, sticky = W)
# Create entry widget to accept Guess
self.guess_ent = Entry(self)
self.guess_ent.grid(row = 2, column = 1, sticky = W)
# Create a space
self.gap1_lbl = Label(self, text = ” “)
self.gap1_lbl.grid(row = 3, column = 0, sticky = W)
“”” Submit Button “””
# Create submit button
self.submit_bttn = Button(self, text = “Submit”, command = self.reveal)
self.submit_bttn.grid(row = 6, column = 0, sticky = W)
# Create a space
self.gap2_lbl = Label(self, text = ” “)
self.gap2_lbl.grid(row = 7, column = 0, sticky = W)
“”” RESET “””
# Create submit button
self.reset_bttn = Button(self, text = “Reset”, command = self.reveal)
self.reset_bttn.grid(row = 6, column = 1, sticky = W)
“”” Display “””
# Create text widget to display welcome_msg
self.display1_txt = Text(self, width = 45, height = 1, wrap = WORD)
self.display1_txt.grid(row = 8, column = 0, columnspan = 2, sticky = W)
# Create text widget to display guess_msg
self.display2_txt = Text(self, width = 45, height = 1, wrap = WORD)
self.display2_txt.grid(row = 9, column = 0, columnspan = 2, sticky = W)
# Create text widget to display result_msg
self.display3_txt = Text(self, width = 45, height = 2, wrap = WORD)
self.display3_txt.grid(row = 10, column = 0, columnspan = 2, sticky = W)
# Create text widget to display tries_msg
self.display4_txt = Text(self, width = 45, height = 2, wrap = WORD)
self.display4_txt.grid(row = 11, column = 0, columnspan = 2, sticky = W)
def reveal(self):
global tries
name = self.name_ent.get()
guess = self.guess_ent.get()
if int(guess) > int(number):
result_msg = “Lower …”
tries += 1
if int(guess) < int(number):
result_msg = “Higher …”
tries += 1
if int(guess) == int(number):
result_msg = “You got it.”
tries += 1
welcome_msg = “Welcome ” + name
guess_msg = ” Your guess was: ” + guess
tries_msg = “You have had ” + str(tries) + ” tries.”
if tries > 10:
welcome_msg = “End of Game.”
guess_msg = “You had too many tires.”
result_msg = ” ”
tries_msg = ” ”
# Display
self.display1_txt.delete(0.0, END)
self.display1_txt.insert(0.0, welcome_msg)
self.display2_txt.delete(0.0, END)
self.display2_txt.insert(0.0, guess_msg)
self.display3_txt.delete(0.0, END)
self.display3_txt.insert(0.0, result_msg)
self.display4_txt.delete(0.0, END)
self.display4_txt.insert(0.0, tries_msg)
# Main manager
root = Tk()
root.title(“Guessing Game”)
root.geometry(“300×225”)
app = Application(root)
root.mainloop()