#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0

    My 2nd Question not working


    Hello,
    Well so I got my first question to work fine, but the second question is just the same as the first one every time. Not all of the code below is needed.
    Code:
    from Tkinter import *
    root = Tk()
    rank=0
    points=0
    root.geometry("900x1000+200+200")
    root.title("Math Game")
    var = StringVar()
    var2= StringVar()
    var3= StringVar()
    var4= StringVar()
    var5= StringVar()
    var6= StringVar()
    var7= StringVar()
    var8= StringVar()
    var9= StringVar()
    var10= StringVar()
    var11= StringVar()
    var12= StringVar()
    var13= StringVar()
    var14= StringVar()
    var15= StringVar()
    label1=Label(root, textvariable=var, relief=RAISED)
    var.set ("Welcome To the Math Game set 1")
    label1.pack()
    label2=Label(root, textvariable=var2, relief=RAISED)
    var2.set("Main Menu")
    label2.pack()
    def callback1():
        label3=Label(root, textvariable=var3, relief=RAISED)
        var3.set("About The Game:")
        label3.pack()
        label4=Label(root, textvariable=var4, relief=RAISED)
        var4.set("The Math Game is a good way to learn how to do math facts as well as doing the problems your self. There is also games with it!")
        label4.pack()
    
    def callback2():
        label5=Label(root, textvariable=var5, relief=RAISED)
        var5.set("Rank: " + str (rank))
        label5.pack()
    
    def callback3():
        label6=Label(root, textvariable=var6, relief=RAISED)
        var6.set("(10 rank = 1 point) Points are used to buy more levels and earn many differnt things. you have " + str (points) + " Points!")
        label6.pack()
    
    def callback4():
        label7=Label(root, textvariable=var7, relief=RAISED)
        var7.set("You are about to start the game!")
        label7.pack()
        Loading=0
        import random
        Questiona = random.randint(1, 10)
        Questionab = random.randint(1, 10)
        Questionaanswerreal = Questiona + Questionab
        label8 = Label(root, textvariable=var12, relief=RAISED)
        var8.set("Question 1: " + str (Questiona) + " + " + str (Questionab) + " = ?")
        label8.pack()
        Questionaansweruser = Entry(root)
        Questionaansweruser.pack()
        def callback5():
            if Questionaansweruser.get() == str(Questionaanswerreal):
                label9 = Label(root, textvariable=var9, relief=RAISED)
                var9.set("Correct! + 25 to your score!")
                label9.pack()
                label10 = Label(root, textvariable=var10, relief=RAISED)
                var10.set("Score: " + str (points))
                label10.pack()
                b4.destroy()
            else:
                label11 = Label(root, textvariable=var11, relief=RAISED)
                var11.set("Incorrect answer! The right answer was, " + str (Questionaanswerreal))
                label11.pack()
                b4.destroy()
        b4 = Button(root, text="submit", width=15, command=callback5)
        b4.pack()
        
        import random
        Questionb = random.randint(1, 10)
        Questionbb = random.randint(1, 10)
        Questionbanswerreal = Questionb + Questionbb
        label12 = Label(root, textvariable=var12, relief=RAISED)
        var12.set("Question 1: " + str (Questionb) + " + " + str (Questionbb) + " = ?")
        label12.pack()
        Questionbansweruser = Entry(root)
        Questionbansweruser.pack()
        def callback6():
            if Questionbansweruser.get() == str(Questionbanswerreal):
                label13 = Label(root, textvariable=var13, relief=RAISED)
                var13.set("Correct! + 25 to your score!")
                label13.pack()
                label14 = Label(root, textvariable=var14
                                , relief=RAISED)
                var14.set("Score: " + str (points))
                label14.pack()
                b6.destroy()
            else:
                label15 = Label(root, textvariable=var15, relief=RAISED)
                var15.set("Incorrect answer! The right answer was, " + str (Questionbanswerreal))
                label15.pack()
                b6.destroy()
        b6 = Button(root, text="submit", width=15, command=callback6)
        b6.pack()
        
        
        
            
    b3=Button(root, text="Play Game", command=callback4)
    b3.pack()
    b2=Button(root, text="Points", command=callback3)
    b2.pack()     
    b1=Button(root, text="Rank", command=callback2)
    b1.pack()
    b=Button(root, text="About The Game", command=callback1)
    b.pack()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    I'm sorry, but I cannot decipher your code.

    My suggestions:
    * Use proper, descriptive variable names, "var12" and "b6" are too cryptic and short
    * Separate the game logic and gui parts
    * Use either CamelCase or underscore_case syntax for your variables. It took me about 10 seconds to decipher what "Questionbansweruser" actually was referring to
    * Put imports on top of your code, no need to import random twice
    http://www.catb.org/esr/faqs/smart-questions.html#code
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481

    Learn about arrays.


    Code:
    import tkinter
    root = tkinter.Tk()
    root.withdraw()
    list_of_16_strvars = [tkinter.StringVar() for i in range(16)]
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo