PYTHON Complete the GuessingGame class, so that it behaves as follows.(THERE IS
ID: 3858648 • Letter: P
Question
PYTHON
Complete the GuessingGame class, so that it behaves as follows.(THERE IS CODE BELOW, IT ISN'T FINISHED BUT IT IS GIVEN FOR US TO COMPLETE)
A. At the beginning, the program picks a random number between 1 and 3. The randint function can be used to do this.
B. The user enters a number, also between 1 and 3, and clicks the "Guess" button.
C. The program displays one of 2 messages: (i) "Your guess is correct"; or (ii) "Your guess is incorrect". You may display the message however you'd like: as a Label which you add to the window, by using the showinfo function, or any other way you can think of.
from urllib.request import urlopen
from html.parser import HTMLParser
from tkinter import *
from random import randint
class GuessingGame:
def __init__(self):
self.number = randint(1,3)
self.window = Tk()
Label(self.window, text='Guess a number between 1 and 3').pack()
self.guess = Entry(self.window, width=5)
self.guess.pack()
b = Button(self.window, text='Guess')
b.pack()
# set up the event handling for when the user clicks the button
# this is the event handler
def message(self, event):
pass # replace this
Explanation / Answer
from urllib.request import urlopen from html.parser import HTMLParser from tkinter import * from random import randint import tkMessageBox class GuessingGame: def __init__(self): self.number = randint(1,3) self.window = Tkinter.Tk() Tkinter.Label(self.window, text='Guess a number between 1 and 3').pack() self.guess = Tkinter.Entry(self.window, width=5) self.guess.pack() b = Tkinter.Button(self.window, text='Guess',command=message) b.pack() # set up the event handling for when the user clicks the button # this is the event handler def message(self, event): number2=randint(1,3) if number2==1: tkMessageBox.showinfo( "you made a correct guess") else: tkMessageBox.showinfo( "oh no! that's a wrong guess!")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.