Mark Lin
Mark Lin

Reputation: 103

my code was working fine until i changed something and now it wont run, whats wrong?

something was wrong with my code and i tried to fix it, now it doesn't run at all, no window pops up, when i stop the code, it says Process finished with exit code -1. i think i changed something from line 24, but not sure what happened. please tell me everything that's wrong with the code, and i believe theres a few that i cant find. thanks

from tkinter import *
import random
p = 0
x = random.randint(5, 12)
y = random.randint(5, 12)

class Starting:
    def __init__(self, master):
        self.master = master
        self.frame = Frame(master, padx=200, pady=200)
        self.frame.grid()
        self.title = Label(self.frame, text="Multi-level Maths Quiz",
                           font=("Helvetica", "20", "bold"))
        self.title.grid(row=0, padx=30, pady=30)
        self.usern = Label(self.frame,text="Please enter a username", font=("16"))
        self.usern.grid(row=1, padx=20, pady=20)
        self.userentry = Entry(self.frame, width=50)
        self.userentry.grid(row=2)
        self.usercont = Button(self.frame, text="Continue", command=self.clear1)
        self.usercont.grid(row=3)

    def clear1(self):
        self.frame.destroy()
        ins1 = Questions(self.master)

class Questions:
    while p < 5:
        def __init__ (self, master):
            self.user_choice = StringVar()
            self.user_choice.set("")
            self.frame = Frame(master, padx=200, pady=200)
            self.frame.grid()
            self.q = Label(self.frame, text="What is {} + {} ?".format(x, y))
            self.q.grid(row=0)
            self.ans = Entry(self.frame, width=50, textvariable=self.user_choice)
            self.ans.grid(row=1)
            self.answer = x+y
            self.sub = Button(self.frame, text="submit", command=self.correct)
            self.sub.grid(row=3)

        def correct(self):
            if int(self.user_choice.get()) == self.answer:
                cor = Label(text="Correct!")
                cor.grid(row=3, pady=50)
                global p
                p += 1
            else:
                inc = Label(text="incorrect")
                inc.grid(row=3)



if __name__ == "__main__":
   root = Tk()
   root.title = ("Maths Quiz")
   instance = Starting(root)
   root.mainloop()

Upvotes: 0

Views: 52

Answers (1)

Parakiwi
Parakiwi

Reputation: 611

The line

while p < 5:

seems strange to me being in a class definition before the initialisation. This means if your p is >= 5 the class will never actually be defined as anything. This does not seem intended. If you comment it out and re-indent your def __init__() and def correct(self): functions the code seems to run again

Upvotes: 1

Related Questions