[Tutor] Wiget

Alan Gauld alan.gauld at freenet.co.uk
Fri Jun 30 14:03:21 CEST 2006


> The problem is at the end where i try and get the number of 
> Tries the user has tried it would just reset everytime the button 
> in clicked, so my question is how would i go about getting 
> the number of times the button is clicked and the anwser is wrong.

>    def number_anwser(self):
>        guess = self.guess_ent.get()
>        guess = int(guess)
>        response = ""
>        tries = 1

You are resetting tries to one every time.
You need to store tries atthe object level by creating a self.tries 
in your init method. You can then increment self.tries in the code 
below.

>        
>        if (guess < the_number):
>            response += "Higher"
>            tries += 1
>        elif (guess > the_number):
>            response += "Lower"
>            tries += 1
>        else:
>            tries = str(tries)

and this would become
             tries = str(self.tries)
that is, you don't want to convert self.tries to a string!

>            response += "Correct! that was my number, \n"
>            response += "You guessed it in just "
>            response += tries
>            response += " tries!"
>  
>        self.response_txt.delete(0.0, END)
>        self.response_txt.insert(0.0, response)
>        

HTH

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list