title: No matter where I put "score = 0" It ignores it and comes up with an error about local variable used before assignment
body: <p>On the python debugger the score is read first and I have tried globalising it to see if anything would work. Also I know the code is messy and there are better ways of doing it but I just want to solve this problem first. This is my first time using stack overflow so I don't know how to link the zip folders to the question if you are wanting to run the code. The folders contain PNG images which are named what is in the array. That bit works without any problems. This is also my first time using tkinter.</p>
  <pre><code>global score
  score = 0
  from tkinter import *
  from PIL import ImageTk, Image
  from random import randint
  import os
  
  
  
  root = Tk()
  root.title(&quot;Flashcard&quot;)
  #root.iconbitmap('c:/')
  root.geometry(&quot;500x500&quot;)
  
  
  global dir_path
  dir_path = os.path.dirname(os.path.realpath(__file__))
  
  def score0():
      score = 0
  
  def scoreadd():
      score = score+1
  
  def scoreminus():
      score = score-1
  
  
  
  
  def checkcorrectcs(comp_questions, random_number,):
      answer = answer_input.get()
      answer = answer.replace(&quot; &quot;, &quot;&quot;)
  
      if answer.lower() == comp_questions[random_number]:
          checked = &quot;Correct&quot;
          scoreadd()
  
      else:
          checked = &quot;Incorrect it was &quot; + comp_questions[random_number].title()
          scoreminus()
      answer_label.config(text=checked)
      
  
  def checkcorrectmaths(maths, random_number2,):
      answer = answer_input.get()
      answer = answer.replace(&quot; &quot;, &quot;&quot;)
      if answer.lower() == math_questions[random_number2]:
          checked = &quot;Correct&quot;
          scoreadd()
      else:
          checked = &quot;Incorrect it was &quot; + math_questions[random_number2].title()
          scoreminus()
      answer_label.config(text=checked)
  
  def checkcorrectph(physics_questions, random_number3,):
      answer = answer_input.get()
      answer = answer.replace(&quot; &quot;, &quot;&quot;)
      if answer.lower() == physics_questions[random_number3]:
          checked = &quot;Correct&quot;
          scoreadd()
      else:
          checked = &quot;Incorrect it was &quot; + physics_questions[random_number3].title()
          scoreminus()
      answer_label.config(text=checked)
  
  def checkcorrectbio(biology_questions, random_number4,):
      answer = answer_input.get()
      answer = answer.replace(&quot; &quot;, &quot;&quot;)
      if answer.lower() == biology_questions[random_number4]:
          checked = &quot;Correct&quot; 
          scoreadd()
      else:
          checked = &quot;Incorrect it was &quot; + biology_questions[random_number4].title()
          scoreminus()
      answer_label.config(text=checked)
  
  def checkcorrectchem(chemistry_questions, random_number5,):
      answer = answer_input.get()
      answer = answer.replace(&quot; &quot;, &quot;&quot;)
      if answer.lower() == chemistry_questions[random_number5]:
          checked = &quot;Correct&quot;
          scoreadd()
      else:
          checked = &quot;Incorrect it was &quot; + chemistry_questions[random_number5].title()
          scoreminus()
      answer_label.config(text=checked)
  
  
  
  
  
  
  
  
  
  
  
  
  
  #Computer science function
  def computer_science():
  
      hide_any_windows()
      computer_science_window.pack(fill=&quot;both&quot;,expand=1)
      title = Label(computer_science_window, text=&quot;Computer science&quot;).pack()
      
      comp_questions = [&quot;and&quot;, &quot;binary&quot;, &quot;denary&quot;, &quot;hexadecimal&quot;, &quot;or&quot;]
      random_number = randint(0, len(comp_questions)-1)
      random_comp_question = f&quot;{dir_path}/ComputerScienceQuestionBank/&quot; + comp_questions[random_number] +&quot;.png&quot;
  
      global comp_question_photo
      comp_question_photo = ImageTk.PhotoImage(Image.open(random_comp_question))
      show_comp_question = Label(computer_science_window, image=comp_question_photo)
      show_comp_question.pack(pady=15)
  
      #answer box
      global answer_input
      answer_input = Entry(computer_science_window, font=(&quot;Comic Sans&quot;, 20))
      answer_input.pack(pady = 15)
  
      confirm_button = Button(computer_science_window, text =&quot;Confirm&quot;, command=lambda: checkcorrectcs(comp_questions, random_number))
      confirm_button.pack(pady=5)
  
      random_button = Button(computer_science_window, text= &quot;New question&quot;, command=computer_science)
      random_button.pack(pady=10)
  
      
  
      global answer_label
      answer_label = Label(computer_science_window, text=&quot;&quot;)
      answer_label.pack(pady=15)  
  
  
  #Maths function
  def maths():
      hide_any_windows()
      maths_window.pack(fill=&quot;both&quot;,expand=1)
      title = Label(maths_window, text=&quot;Maths&quot;).pack()
  
      math_questions = [&quot;144&quot;, &quot;test2&quot;]
      random_number2 = randint(0, len(math_questions)-1)
      random_math_question = f&quot;{dir_path}/MathQuestionBank/&quot; + math_questions[random_number2] +&quot;.png&quot;
  
      global math_question_photo
      math_question_photo = ImageTk.PhotoImage(Image.open(random_math_question))
      show_math_question = Label(maths_window, image=math_question_photo)
      show_math_question.pack(pady=15)
  
      #answer box
      global answer_input
      answer_input = Entry(maths_window, font=(&quot;Comic Sans&quot;, 20))
      answer_input.pack(pady = 15)
  
      confirm_button = Button(maths_window, text =&quot;Confirm&quot;, command=lambda: checkcorrectcs(math_questions, random_number2))
      confirm_button.pack(pady=5)
  
      random_button = Button(maths_window, text= &quot;New question&quot;, command=maths)
      random_button.pack(pady=10)
  
      
  
      global answer_label
      answer_label = Label(maths_window, text=&quot;&quot;)
      answer_label.pack(pady=15)  
  
  
  
  #Physics function
  def physics():
      hide_any_windows()
      physics_window.pack(fill=&quot;both&quot;,expand=1)
      title = Label(physics_window, text=&quot;Maths&quot;).pack()
  
      physics_questions = [&quot;9.81&quot;, &quot;test3&quot;, &quot;quarks&quot;, &quot;speedoflight&quot;]
      random_number3 = randint(0, len(physics_questions)-1)
      random_physics_question = f&quot;{dir_path}/PhysicsQuestionBank/&quot; + physics_questions[random_number3] +&quot;.png&quot;
  
      global physics_question_photo
      physics_question_photo = ImageTk.PhotoImage(Image.open(random_physics_question))
      show_physics_question = Label(physics_window, image=physics_question_photo)
      show_physics_question.pack(pady=15)
  
      #answer box
      global answer_input
      answer_input = Entry(physics_window, font=(&quot;Comic Sans&quot;, 20))
      answer_input.pack(pady = 15)
  
      confirm_button = Button(physics_window, text =&quot;Confirm&quot;, command=lambda: checkcorrectph(physics_questions, random_number3))
      confirm_button.pack(pady=5)
  
      random_button = Button(physics_window, text= &quot;New question&quot;, command=physics)
      random_button.pack(pady=10)
  
      
  
      global answer_label
      answer_label = Label(physics_window, text=&quot;&quot;)
      answer_label.pack(pady=15)
  
  
  
  #Biology function
  def biology():
      hide_any_windows()
      biology_window.pack(fill=&quot;both&quot;,expand=1)
      title = Label(biology_window, text=&quot;Biology&quot;).pack()
  
      biology_questions = [&quot;test3&quot;, &quot;test4&quot;]
      random_number4 = randint(0, len(biology_questions)-1)
      random_biology_question = f&quot;{dir_path}/BiologyQuestionBank/&quot; + biology_questions[random_number4] +&quot;.png&quot;
  
      global biology_question_photo
      biology_question_photo = ImageTk.PhotoImage(Image.open(random_biology_question))
      show_biology_question = Label(biology_window, image=biology_question_photo)
      show_biology_question.pack(pady=15)
  
      #answer box
      global answer_input
      answer_input = Entry(biology_window, font=(&quot;Comic Sans&quot;, 20))
      answer_input.pack(pady = 15)
  
      confirm_button = Button(biology_window, text =&quot;Confirm&quot;, command=lambda: checkcorrectbio(biology_questions, random_number4))
      confirm_button.pack(pady=5)
  
      random_button = Button(biology_window, text= &quot;New question&quot;, command=biology)
      random_button.pack(pady=10)
  
      
  
      global answer_label
      answer_label = Label(biology_window, text=&quot;&quot;)
      answer_label.pack(pady=15)
  
  
  
  #Chemistry function
  def chemistry():
      hide_any_windows()
      chemistry_window.pack(fill=&quot;both&quot;,expand=1)
      title = Label(chemistry_window, text=&quot;Chemistry&quot;).pack()
  
      chemistry_questions = [&quot;jameschadwick&quot;, &quot;loweractivationenergy&quot;, &quot;mendeleev&quot;, &quot;postive&quot;, &quot;protondonors&quot;,]
      random_number5 = randint(0, len(chemistry_questions)-1)
      random_chemistry_question = f&quot;{dir_path}/ChemistryQuestionBank/&quot; + chemistry_questions[random_number5] +&quot;.png&quot;
  
      global chemistry_question_photo
      chemistry_question_photo = ImageTk.PhotoImage(Image.open(random_chemistry_question))
      show_chemistry_question = Label(chemistry_window, image=chemistry_question_photo)
      show_chemistry_question.pack(pady=15)
  
      #answer box
      global answer_input
      answer_input = Entry(chemistry_window, font=(&quot;Comic Sans&quot;, 20))
      answer_input.pack(pady = 15)
  
      confirm_button = Button(chemistry_window, text =&quot;Confirm&quot;, command=lambda: checkcorrectchem(chemistry_questions, random_number5))
      confirm_button.pack(pady=5)
  
      random_button = Button(chemistry_window, text= &quot;New question&quot;, command=chemistry)
      random_button.pack(pady=10)
  
      
  
      global answer_label
      answer_label = Label(chemistry_window, text=&quot;&quot;)
      answer_label.pack(pady=15)
  
  
  
  
  
  
  
  
  
  
  
  
  
  def hide_any_windows():
      for widget in computer_science_window.winfo_children():
          widget.destroy()
  
      for widget in maths_window.winfo_children():
          widget.destroy()
  
      for widget in physics_window.winfo_children():
          widget.destroy()
  
      for widget in biology_window.winfo_children():
          widget.destroy()
  
      for widget in chemistry_window.winfo_children():
          widget.destroy()
  
  
      computer_science_window.pack_forget()
      maths_window.pack_forget()
      physics_window.pack_forget()
      biology_window.pack_forget()
      chemistry_window.pack_forget()
  
  
  #menu
  my_menu = Menu(root)
  root.config(menu=my_menu)
  
  
  
  
  #Subjects for the menu
  subjects_menu = Menu(my_menu)
  my_menu.add_cascade(label = &quot;Subjects&quot;, menu=subjects_menu)
  subjects_menu.add_command(label=&quot;Computer science&quot;, command=computer_science)
  subjects_menu.add_command(label=&quot;Maths&quot;, command=maths)
  subjects_menu.add_command(label=&quot;Physics&quot;, command=physics)
  subjects_menu.add_command(label=&quot;Biology&quot;, command=biology)
  subjects_menu.add_command(label=&quot;Chemistry&quot;, command=chemistry)
  subjects_menu.add_separator()
  subjects_menu.add_command(label=&quot;Exit&quot;, command=root.quit)
  
  
  
  #Making the window
  computer_science_window = Frame(root, width=500, height=500)
  maths_window = Frame(root, width=500, height=500)
  physics_window = Frame(root, width=500, height=500)
  biology_window = Frame(root, width=500, height=500)
  chemistry_window = Frame(root, width=500, height=500)
  
  # all_windows = [computer_science_window, maths_window, physics_window, biology_window, chemistry_window]
  
  
  
          
  root.mainloop()
  </code></pre>