Question

Need Guessing game (number) with graphics (simple one) / python language

Need Guessing game (number) with graphics (simple one) / python language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Program:

from random import randint
from tkinter import *
import tkinter.messagebox

class GuessingGame:
   def __init__(self):
       self.number = randint(1,10)
       self.window = Tk()
       Label(self.window, text='Guess a number between 1 and 10').pack()
       self.guess = Entry(self.window, width=10)
       self.guess.pack()
       b = Button(self.window, text='Guess', command = self.message)
       b.pack()
       # set up the event handling for when the user clicks the button
       self.window.mainloop()
      
   # this is the event handler
   def message(self):
       # Fetching value entered in text box
       try:
           # Getting number
           guessNumber = int(self.guess.get());
          
           # Comparing number
           if guessNumber == self.number:
               # If both are matched
               tkinter.messagebox.showinfo("Guessing Game", " Congrats!!! Correct Guess ");
           else:
               # If not matched
               tkinter.messagebox.showinfo("Guessing Game", " Sorry!!! It is an incorrect Guess ");
       # If user enters any value other than integer
       except:
           # Displaying message box
           tkinter.messagebox.showinfo("Guessing Game", " Error!!! Please enter only integers ");
      
# Calling function      
game = GuessingGame();


____________________________________________________________________________________________

Sample Run:

Guessing Game EX tk Guess a number between 1 and 10 0 Congrats!!! Correct Guess Guess OK

Add a comment
Know the answer?
Add Answer to:
Need Guessing game (number) with graphics (simple one) / python language
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT