title: UnboundLocalError: local variable 'qn' referenced before assignment
body: <p>I'm getting the error &quot;UnboundLocalError: local variable 'qn' referenced before assignment&quot; on running the code. Why is that? How can I correct it? I'm new to tkinter so please try to keep it simple. This is part of the code for a game I was writing. It would be a great help if I could get an answer soon</p>
  <pre><code>from tkinter import * 
  from tkinter import messagebox 
  from io import StringIO
  
  root = Tk() 
  root.title(&quot;Captain!&quot;) 
  root.geometry(&quot;660x560&quot;)                                   
  
  qn = '''1$who are you?$char1$i am joe$3$i am ben$2
  2$what are you?$char2$i am a person$1$i am nobody$3
  3$how are you?$char3$i am fine$2$i'm alright$1'''
  var = '''1$10$-35$20$15$-20
  2$9$7$30$-5$-15
  3$10$-25$-15$10$5'''
  
  class Game :
      def __init__(self): 
          self.m_cur = {1:[&quot;Military&quot;,50]}
          self.c_cur = {1:[&quot;People's&quot;,50]}
          self.r_cur = {1:[&quot;Research&quot;,50]}
          self.i_cur = {1:[&quot;Industrial&quot;,50]}
          self.p_cur = {1:[&quot;Research&quot;,50]}
  
      #function to clear all widgets on screen when called
      def clear(self):
          for widget in root.winfo_children():
              widget.destroy()
      
      #function to quit the window
      def exit(self):
          msg = messagebox.askquestion(&quot;Thank you for playing&quot;,&quot;Are you sure you want to exit?&quot;)
          if msg == &quot;yes&quot; :
              root.destroy()
          else:
              Game.main(self)
  
      #start function
      def start(self):
          Label(root,text=&quot;Hello, what should we call you?&quot;,font=(&quot;segoe print&quot;,20)).grid(row=0,column=0)
          name = Entry(root,width=20)
          name.grid(row=1,column=0)
          Button(root,text=&quot;Enter&quot;,font=(&quot;segoe print&quot;,20),command=lambda: Game.main(self)).grid(row=1,column=1)
          self.name=name.get()
  
      #main function
      def main(self): 
          Game.clear(self)
          Label(root,text=&quot;Welcome to the game&quot;,font=(&quot;segoe print&quot;,20)).grid(row=0,column=0)
          Label(root,text='What do you want to do?',font=(&quot;segoe print&quot;,20)).grid(row=1,column=0)
          Button(root,text=&quot;Start Game&quot;,font=(&quot;segoe print&quot;,20),command=lambda: Game.qn_func(self,1)).grid(row=2,column=0)
          Button(root,text=&quot;Exit Game&quot;,font=(&quot;segoe print&quot;,20),command=lambda: Game.exit(self)).grid(row=3,column=0)
      
      #function to check variables and display game over
      def game_over(self,x_cur):
          if x_cur[1][1]&lt;=0 or x_cur[1][1]&gt;=100 : #condition to check game over
              Game.clear(self)
              Label(root,text=x_cur)
              Label(root,text=&quot;GAME OVER&quot;,font=(&quot;ariel&quot;,20)).place(relx=0.5,rely=0.5,anchor=CENTER)
              Button(root,text=&quot;Continue&quot;,font=(&quot;segoe print&quot;,20),command=lambda: Game.main(self)).place(relx=0.5,rely=0.6)
  
      #function to display question and variables
      def qn_func(self,qn_num) :
          Game.clear(self)
          #accessing the questions
          q_file = StringIO(qn)
          #reading the question, options, next qn numbers and the character name from the file
          qn_list = q_file.readlines() 
          qn = qn_list[qn_num-1].strip().split(&quot;$&quot;)[1]
          char_name = qn_list[qn_num-1].strip().split(&quot;$&quot;)[2]
          qn1 = qn_list[qn_num-1].strip().split(&quot;$&quot;)[3]
          qn2 = qn_list[qn_num-1].strip().split(&quot;$&quot;)[5]
          n_qn1 = int(qn_list[qn_num-1].strip().split(&quot;$&quot;)[4])
          n_qn2 = int(qn_list[qn_num-1].strip().split(&quot;$&quot;)[6])
          #displaying the character name and the question as a label frame widget with character name as parent
          label_frame = LabelFrame(root,text = char_name,font = (&quot;segoe print&quot;,20))
          label = Label(label_frame,text = qn,font = (&quot;segoe print&quot;,20))
          label_frame.place(relx=0.5,rely=0.5,anchor=CENTER)
          label.pack()
          q_file.close()
          #accessing variables
          v_file = StringIO(var)
          #reading values of variables from file
          v_list = v_file.readlines()
          self.r_cur[1][1] += int(v_list[qn_num-1].strip().split(&quot;$&quot;)[1])
          self.c_cur[1][1] += int(v_list[qn_num-1].strip().split(&quot;$&quot;)[2])
          self.i_cur[1][1] += int(v_list[qn_num-1].strip().split(&quot;$&quot;)[3])
          self.m_cur[1][1] += int(v_list[qn_num-1].strip().split(&quot;$&quot;)[4])
          self.p_cur[1][1] += int(v_list[qn_num-1].strip().split(&quot;$&quot;)[5])
          #running each variable through game_over to see if you are dead
          Game.game_over(self,self.r_cur)
          Game.game_over(self,self.c_cur)
          Game.game_over(self,self.i_cur)
          Game.game_over(self,self.m_cur)
          Game.game_over(self,self.p_cur)
          #defining the Doublevar variables
          s_var1 = DoubleVar()
          s_var2 = DoubleVar()
          s_var3 = DoubleVar()
          s_var4 = DoubleVar()
          s_var5 = DoubleVar()
          #setting the values in the scales
          s_var1.set(self.r_cur[1][1])
          s_var2.set(self.c_cur[1][1])
          s_var3.set(self.i_cur[1][1])
          s_var4.set(self.m_cur[1][1])
          s_var5.set(self.p_cur[1][1])
          #variables as scale widgets
          scale1 = Scale(root,from_=100,to=0,orient=VERTICAL,sliderlength=10,variable=s_var1)
          scale2 = Scale(root,from_=100,to=0,orient=VERTICAL,sliderlength=10,variable=s_var2)
          scale3 = Scale(root,from_=100,to=0,orient=VERTICAL,sliderlength=10,variable=s_var3)
          scale4 = Scale(root,from_=100,to=0,orient=VERTICAL,sliderlength=10,variable=s_var4)
          scale5 = Scale(root,from_=100,to=0,orient=VERTICAL,sliderlength=10,variable=s_var5)
          #displaying the scale widgets on the screen
          scale1.grid(row=0,column=0)
          scale2.grid(row=0,column=1)
          scale3.grid(row=0,column=2)
          scale4.grid(row=0,column=3)
          scale5.grid(row=0,column=4)
          #disabling the scales
          scale1.config(state=DISABLED)
          scale2.config(state=DISABLED)
          scale3.config(state=DISABLED)
          scale4.config(state=DISABLED)
          scale5.config(state=DISABLED)
          v_file.close()
          #displaying the buttons on the screen
          Button(root,text=qn1,command=lambda: Game.qn_func(self,n_qn1)).place(relx=0.2,rely=0.7,anchor=W,width=200,height=50)
          Button(root,text=qn2,command=lambda: Game.qn_func(self,n_qn2)).place(relx=0.8,rely=0.7,anchor=E,width=200,height=50)
  
  game = Game()
  game.start()
  root.mainloop()
  </code></pre>