[Tutor] Help python coding not working

super krital super_krital_000 at hotmail.com
Tue May 8 08:07:33 CEST 2007


Hi need to get this program running for school so my teacher said to use the 
forum etc. Its working fine except its not comparing my guess with the 
result. im only having trouble with the second half or def update_text_count 
can you please help find a solution so my program works please. i dont see 
why it is not.

#Krital
#Guess my number game assignment

from Tkinter import *
import random


class Application(Frame):
    """GUI Application for game difficulty."""
    def __init__(self,master):
        """initialise frame."""
        Frame.__init__(self,master)
        self.grid()
        self.bttn3_clicks=0
        self.create_widgets()

    #Creating user instruction labels

    def create_widgets(self):
        #create widgets for difficulty levels
        #create description label
        Label(self,
              text="Choose your difficalty level"
              ).grid(row=0,column=0,sticky=W)

        Label(self,
              text="Select one:"
              ).grid(row=1,column=0,sticky=W)


#Since only one radio button can be selected, they share one special object 
that
#reflects which of the radio buttons is selected. This object needs to be an 
instance of the
#StringVar class frpm the Tkinter module, which allows a string to be stored 
and retrieved.

        #create a variable for game difficulty
        self.difficulty = StringVar()

        #Create the Easy radio button
        Radiobutton(self,
                    text="Easy",
                    variable=self.difficulty,
                    value="easy",
                    command=self.random_number
                    ).grid(row=2, column=0, sticky=W)


        Radiobutton(self,
                    text="Medium",
                    variable=self.difficulty,
                    value="medium",
                    command=self.random_number
                    ).grid(row=3, column=0, sticky=W)

        Radiobutton(self,
                    text="Hard",
                    variable=self.difficulty,
                    value="hard",
                    command=self.random_number
                    ).grid(row=4, column=0, sticky=W)

        self.bttn = Button(self)
        self.bttn["text"]="Exit"
        self.bttn["command"]=self.stop
        self.bttn.grid(row=23,column=1,sticky=W)

        self.bttn2 = Button(self)
        self.bttn2["text"]="Reset"
        self.bttn2["command"]=self.reset
        self.bttn2.grid(row=23,column=0,sticky=W)

        #using the Grid layout manager to be specific about the labels 
placement
        self.inst_lbl=Label(self,text="Please guess a number.")
        self.inst_lbl.grid(row=6,column=0,columnspan=2, sticky=W)
        #Sticky W means the label is forced to the west-or is left 
justified!
        #sticky can be N,S,E or W

        #creating an entry widget to make a guess
        self.guess=Entry(self)
        self.guess.grid(row=7,column=0,sticky=W)
        #self.guess.focus_set()

        self.bttn3=Button(self,text="Total 
Goes=0",relief=FLAT,command=self.update_text_count)
        self.bttn3.grid(row=8,column=0, sticky=W)

		#create a button to enter the guess

        self.enter_bttn=Button(self)
        self.enter_bttn["text"]="Enter"
        self.enter_bttn["command"]=self.update_text_count
        self.enter_bttn.grid(row=7,column=1,sticky=W)


        #Createing the text box to display the user's selection
        self.message_txt=Text(self,width=40,height=5,wrap=WORD)
        self.message_txt.grid(row=11,column=0, columnspan=3)


    def random_number(self):    #creating a definition for all the random 
numbers to be generated in
        difficulty=self.difficulty.get()
        if difficulty=="easy":
            self.number = random.randrange(10)+1
        elif difficulty=="medium":
            self.number = random.randrange(50)+1
        else:
            self.number = random.randrange(100)+1

    def update_text_count(self):
        #Update counter and maximum user guesses
        self.bttn3_clicks+=1
        self.bttn3["text"]="Total Goes= "+str(self.bttn3_clicks)
        difficulty=self.difficulty.get()
        if difficulty=="easy":
            if self.bttn3_clicks > 3:
                message="You have run out of guesses. The number you wanted 
was ", self.number
        if difficulty=="medium":
            if self.bttn3_clicks > 6:
                message="You have run out of guesses. The number you wanted 
was ", self.number
        if difficulty=="hard":
            if self.bttn3_clicks > 9:
                message="You have run out of guesses. The number you wanted 
was ", self.number

        #Update text area and display user's game difficulty
        self.guess = int(self.guess.get())#Having trouble on this line!!!

        if self.guess>self.number:
            message=str(self.guess)+" is too High.Guess again"
        if self.guess<self.number:
            message=str(self.guess)+" is too low. Guess again"

        if self.guess==self.number:
            message=str(self.guess)+" is correct!",
            "goes= ",self.bttn3_clicks

    #deleting any text in the text box and inserting the string just created 
as well as disabling the box
        self.message_txt.config(state=NORMAL)
        self.message_txt.delete(0.0, END)
        self.message_txt.insert(0.0, message)
        self.message_txt.config(state=DISABLED)

    def reset(self):    #creating definition to reset the game
        number=0
        self.bttn3_clicks=0
        self.create_widgets()

    def stop(self):
        root.destroy()

        #wrap the program

root=Tk()
root.title("~*Guess the Number Game*~")
root.geometry("500x400")

app=Application(root)

root.mainloop()

_________________________________________________________________
Advertisement: Senior Management roles paying $80k+ Search now at 
www.seek.com.au 
http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fninemsn%2Eexecutive%2Eseek%2Ecom%2Eau%2F%3Ftracking%3Dsk%3Ahet%3Ase%3Anine%3A0%3Ahot%3Atext&_t=763838044&_r=seek_may07_snrmanagement&_m=EXT



More information about the Tutor mailing list