Reputation: 55
I'm fairly new to python and am working on a CS problem where I have to design a simple number guessing game. The game must also contain a Tkinter Label widget that explains the rules of the game (pick a number between 1 - 100), an Entry widget that takes the users' guess, and a button to call a method within the class that 1) Determines if the guess is higher or lower than a 'secret' randint 2) Guide the user towards the correct by guessing higher or lower, and 3) Congratulate the user once they get the correct answer.
I keep running into an error after my conditional where the showinfo dialog box infinitely loops. I can't figure out where I am going wrong, but here is my code where I start by importing the following:
from random import randint
from tkinter import Tk, Frame, Button, Entry, Label, END
from tkinter.messagebox import showinfo
class Game(Frame):
'A guessing game where a user picks a number from 1 - 100'
def __init__(self, parent=None):
Frame.__init__(self, parent)
self.pack()
Label(self, text = "Pick an integer from 1 - 100").grid(row = 0, column = 0)
self.answer = Entry(self, width=20)
self.answer.grid(row=0, column=1)
Button(self, text='Enter', command = self.compute).grid(row = 1, column = 0)
def compute(self):
secret = randint(1, 100)
answer = int(self.answer.get())
while secret != answer:
if answer > secret:
showinfo(message='Guess a lower integer')
self.answer.delete(0, END)
else:
showinfo(message='Guess a higher integer')
self.answer.delete(0, END)
showinfo(message='You are correct!')
If anyone can explain why I am running into the loop I will likely be able to figure out the rest.
Upvotes: 1
Views: 553
Reputation: 55
Thanks @Rinzler - here's what I got (if any one would like to play a useless 'guess a number game'). Cheers...
from random import randint
from tkinter import Tk, Frame, Button, Entry, Label, END
from tkinter.messagebox import showinfo
class Game(Frame):
'A guessing game where a user picks a number from 1 - 100'
def __init__(self, parent=None):
Frame.__init__(self, parent)
self.pack()
Label(self, text = "Pick an integer from 1 - 100").grid(row = 0, column = 0)
self.answer = Entry(self, width=20)
self.answer.grid(row=0, column=1)
Button(self, text='Enter', command = self.compute).grid(row = 1, column = 0)
self.secret = randint(1,100)
def compute(self):
answer = int(self.answer.get())
while self.secret != answer:
if answer > self.secret:
showinfo(message='Guess a lower integer')
self.answer.delete(0, END)
break
else:
showinfo(message='Guess a higher integer')
self.answer.delete(0, END)
break
if self.secret == answer:
showinfo(message='You got it! :)')
Upvotes: 1
Reputation: 15837
The problem is that once you enter the while loop because of the satisfied condition secret != answer
, you won't exit, because the statement will continue to be evaluate True, that's why the dialog continues to show up.
To solve this, you can for example put a break
after the call to showinfo
:
while secret != answer:
if answer > secret:
showinfo(message='Guess a lower integer')
self.answer.delete(0, END)
break
else:
showinfo(message='Guess a higher integer')
self.answer.delete(0, END)
break
showinfo(message='You are correct!')
Note the last showinfo
will always be executed, but I am sure you are able to change this.
Upvotes: 3