Python Tkinter Quiz score system -
i making quiz in python (using tkinter). stuck on score system have made variable called 'score' when try add + 1 score gets error. have tried define before procedure still doesn't work.
import tkinter tk window = tk.tk() window.title("6 questions") window.geometry("500x150") def inst(): t = tk.label(window, text="all need answer each question either '1, 2, 3' or actual word.") t.pack() def start(): score = 0 q1 = tk.tk() q1.title("question 1") q = tk.label(q1, text="what type of input holds whole numbers?") q.pack() = tk.label(q1, text="1.) int") a.pack() b = tk.label(q1, text="2.) string") b.pack() c = tk.label(q1, text="3.) float") c.pack() ans = tk.entry(q1, width=40) ans.pack() def qu1(): deranswer = ans.get() if deranswer == "1" or deranswer == "int": crr = tk.label(q1, text="correct!", fg="green") crr.pack() score = score + 1 q2 = tk.tk() q2.title("question 2") qw = tk.label(q2, text="choose correct code:") qw.pack() ans21 = tk.label(q2, text="1.) print hello world") ans21.pack() ans32 = tk.label(q2, text="2.) print hello world()") ans32.pack() ans43 = tk.label(q2, text="3.) print('hello world')") ans43.pack() ans2 = tk.entry(q2, width=40) ans2.pack() def qu2(): deranswer = ans2.get() if deranswer == "3" or deranswer == "print('hello world')": crr = tk.label(q2, text="correct!", fg="green") crr.pack() score = score + 1 q3 = tk.tk() q3.title("question 3") qu3 = tk.label(q3, text="what variable?") qu3.pack() ans56 = tk.label(q3, text="1.) stores food") ans56.pack() ans65 = tk.label(q3, text="2.) name stores piece of data") ans65.pack() ans45 = tk.label(q3, text="3.) displays word") ans45.pack() ans25 = tk.entry(q3, width=40) ans25.pack() def qu2(): deranswer = ans25.get() if deranswer == "2" or deranswer == "a name stores piece of data": crr = tk.label(q3, text="correct!", fg="green") crr.pack() score = score + 1 dis = tk.label(q3, text=score, fg="blue") else: incr = tk.label(q3, text="incorrect!", fg="red") incr.pack() sub = tk.button(q3, text="submit", command=qu2) sub.pack() else: incr = tk.label(q2, text="incorrect!", fg="red") incr.pack() sub = tk.button(q2, text="submit", command=qu2) sub.pack() else: incr = tk.label(q1, text="incorrect!", fg="red") incr.pack() sub = tk.button(q1, text="submit", command=qu1) sub.pack() greet = tk.label(window, text="welcome 6 question quiz.") greet.pack() start = tk.button(window, command=start, text="start") start.pack() instr = tk.button(window, text="instructions", command=inst) instr.pack() end = tk.button(window, text="exit", command=exit) end.pack()
from comment traceback:
line 27, in qu1 score = score + 1 unboundlocalerror: local variable 'score' referenced before assignment
you trying add score
, +1
itself, score
not defined within function. variables have scope. read more in the python documentation.
Comments
Post a Comment