-3

I am having an issue figuring out why my code fails when including a Docstring, Specifically on the function select_level(): If i remove the Docstring and comment out instead it works fine, however if i include the Docstring i get the following error on the terminal:

(NameError: name 'select_level' is not defined)

def select_level():
    """Defines how a player selects a difficulty, selects questions 
       and answers depending on user input,outputs selections.
    """
    print ("Ready Player One! Select a level.")
    level_name = raw_input("Type in easy, medium or hard\n").lower()
    if level_name=="easy":
        level(easy_level, blanks, easy_answers)
    elif level_name=="medium":
        level(medium_level, blanks, medium_answers)
    elif level_name=="hard":
        level(hard_level, blanks, hard_answers)
    else:
        print ("Please select easy, medium or hard")
    print select_level()
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • 1
    It's not docstring it's the indentation – Nir Alfasi Aug 31 '18 at 00:01
  • 4
    That looks fine to me. Make sure that you aren't mixing tabs & spaces in your indentation. – PM 2Ring Aug 31 '18 at 00:16
  • PM 2Ring thanks for the advice, i went back and made sure to rewrite my code and ensured i was only using the tab, it is working now, thanks again ! – user10108802 Aug 31 '18 at 02:07
  • @user10108802 for the record; it's recommended that (generally 4) _spaces_ be used to indent Python code. You can configure most editors to turn one keystroke of the `Tab` key into four spaces. – Jules Aug 31 '18 at 03:47

2 Answers2

0

I have a few suggestions:

Make sure select_level() is being called correctly and that it is, indeed defined.

I ask because it says

(The error says (NameError: name 'select_level' is not defined).

Also, have you tried adding parentheses on the last line?

print(select_level())
Yogurt
  • 2,913
  • 2
  • 32
  • 63
0

Your problem is most likely indentation. Do you have multiple functions in your code? If so, make sure that the select_level function is not nested inside another function. I couldn't reproduce your error running your code as you provided it. But when I nested your function inside another function like this:

def my_other_function():
  print "This is my other function"

  def select_level():
    """Defines how a player selects a diffuclity, selects questions 
      and answers depending on user input,outputs selections.
    """
  print ("Ready Player One! Select a level.")
  level_name = raw_input("Type in easy, medium or hard\n").lower()
  if level_name=="easy":
      level(easy_level, blanks, easy_answers)
  elif level_name=="medium":
      level(medium_level, blanks, medium_answers)
  elif level_name=="hard":
      level(hard_level, blanks, hard_answers)
  else:
      print ("Please select easy, medium or hard")

And then try to call the select_level function it breaks with the same error you are getting. See how the second function is indented further than the first function? That is causing the error.

Because the function is nested inside the other function it is out of scope. For more information on scope in python, give this page a read.

In addition, there are two other problems with the code sample you provided.

  1. Remove the indent on the last line of your code. So it looks like this:

    def select_level():
      """Defines how a player selects a diffuclity, selects questions 
      and answers depending on user input,outputs selections.
      """
      print ("Ready Player One! Select a level.")
      level_name = raw_input("Type in easy, medium or hard\n").lower()
      if level_name=="easy":
        level(easy_level, blanks, easy_answers)
      elif level_name=="medium":
        level(medium_level, blanks, medium_answers)
      elif level_name=="hard":
        level(hard_level, blanks, hard_answers)
      else:
        print ("Please select easy, medium or hard")
    print select_level()
    
  2. When you use a print statement on a function it's going to try and print the return value. Your function does not have a return value, so you don't need the print statement at all. Remove it so your code looks like this:

    def select_level():
      """Defines how a player selects a diffuclity, selects questions 
      and answers depending on user input,outputs selections.
      """
      print ("Ready Player One! Select a level.")
      level_name = raw_input("Type in easy, medium or hard\n").lower()
      if level_name=="easy":
        level(easy_level, blanks, easy_answers)
      elif level_name=="medium":
        level(medium_level, blanks, medium_answers)
      elif level_name=="hard":
        level(hard_level, blanks, hard_answers)
      else:
        print ("Please select easy, medium or hard")
    select_level()
    
CSBatchelor
  • 174
  • 6