title: How to error check a function containing a SQL query with the placeholder %s
body: <p>I am working on a project for school and run into a crossroad. I have a bunch of queries in a python script that a user can search when running the script. A couple of these function are using %s as a placeholder so that the user can enter a value.</p>
  
  <p>However, I want to check that what they enter is actually in my database. For instance, if you ask a user for a movie genre, it should produce an error if they enter something that is not in my tables.</p>
  
  <p>I have spent a few days trying to find a way to do it, with no luck. I am not the greatest with functions, and I get things mixed up some times.</p>
  
  <p>To simplify things I copied one of my queries requiring user input for testing. I thought I had come up with a solution on how to error check, but no luck. I have pasted the code below. If you know what I am missing, your help would be much appreciated.</p>
  
  <pre><code>#!/usr/bin/python36
  
  #Modules
  ############################
  import psycopg2
  import sys
  import os
  
  #Open Database
  ############################
  global cursor
  
  #def OpenDatabase():
  try:
          connection = psycopg2.connect(database='Blockbuster36', user='dbadmin')
          cursor = connection.cursor()
  except psycopg2.DatabaseError:
          print("No connection to database.")
          sys.exit(1)
  
  #Functions
  ###########################
  def Show_Tuples():
          tuple = cursor.fetchone()
          while tuple != None:
                  print(tuple)
                  tuple = cursor.fetchone()
  
  #3) Display all films of a certain genre
  def Qry3_Film_of_Genre(Genre_Choice):
          Qry3 = '''select Film_Title, Genre_Name from Film 
                  join Film_Genre 
                  on Film.Film_ID = Film_Genre.Film_ID 
                  join Genre 
                  on Film_Genre.Genre_ID = Genre.Genre_ID 
                  where Genre_Name = %s;'''
  
          cursor.execute(Qry3, (Genre_Choice,))
          Show_Tuples()
  
  def Qry3_Error_Check(Genre_Choice):
          try:
                  Qry3_ec = "select Genre_ID, Genre_Name from Genre where Genre_Name = %s;"
                  cursor.execute(Qry3_ec, (Genre_Choice,))
                  results = cursor.fetchone()
                  if results[0] &gt; 0:
                          print()
                          Qry3_Film_of_Genre(Genre_Choice)
  #               else:
                  elif results[0] == None:
                          print("This Genre does not exist")
                          Genre_Choice = input("Please enter the Genre name to filter movies by: ")
          except psycopg2.Error as query_issue:
                  print("Something wrong with the query", query_issue)
          except Exception as unkown:
                  print("Error: Something else went wrong")
                  print("Here is the issue: ", unkown)
  
  #Main Code
  ##########################
  Genre_Choice = input("Please enter the Genre name to filter movies by: ")
  Check_Genre = Qry3_Error_Check(Genre_Choice)
  #print("Function Return: ", Check_Genre)
  #print()
  
  #print(Check_Genre)
  #if Check_Genre == None:
  #       print("This Genre does not exist")
  #       Genre_Choice = input("Please enter the Genre name to filter movies by: ")
  #       Check_Genre = Qry3_Error_Check(Genre_Choice)
  #elif Check_Genre != None:
  #       Qry3_Film_of_Genre(Genre_Choice)
  
  #while Check_Genre != Genre_Choice:
  #       print("This Genre does not exist")
  #       Genre_Choice = input("Please enter the Genre name to filter movies by: ")
  #       Check_Genre = Qry3_Error_Check(Genre_Choice)
  #if Check_Genre == None:
  #       Qry3_Film_of_Genre(Genre_Choice)
  
  #Close Cursor and Database
  ###################################
  cursor.close()
  connection.close()
  </code></pre>
  
  <p>Essentially I want the error message to keep printing, along with entering another genre name, until a valid genre is entered. Right now it keeps saying it is wrong even if enter a valid genre name and get output with the Qry3_Error_Check function.</p>
  
  <p>Once the user has entered a valid genre name, based on the error-checking function query, then the original query will appear.</p>
  
  <hr>
  
  <p>I have made some progress, entering a valid genre now works. However, when entering an invalid genre name it jumps to the general except and prints the error "NoneType object is not subscriptable." Obviously, there are no rows that match, thus the NoneType error. However, it should re-prompt the user in the elif statement above. What do enter as an elif statement so that the user is re-prompted for a valid genre name?
  Note: I commented out the bulk of my main code for now.</p>