Amari Hanes
Amari Hanes

Reputation: 1

Python Quiz trouble with error message saying not defined?

The problem with the code below is that when I defined questions towards the bottom I keep getting this error:

Also if there are any ways to make this code shorter I'd greatly appreciate the help overall I believe I have shortened this down as much as possible, but since I am new to python I will gladly accept any feedback thank you.

Traceback (most recent call last): File "python", line 63, in NameError: name 'select_level' is not defined

#lines 4 through 21 contains my questions and answers 

easy_questions= ["""What is 20 + 2?__1__", "What is the capital of Georgia?__2__","If john has $4 and buys a candy bar for $3.50, how much money does he have left over?__3__",
"What is the name of the NBA basketball team in Georgia?__4__","What is the name of the NFL team in Georgia?__5__",
"How many toys do I have if I sell 3, but had 8 toys originally?__6__","How many Bad Boy movies are there?__7__",
"How many Fast Furious movies are there?__8__","What is 10 * 5?__9__","What city does the UGA team play in?__10__"""]

medium_questions= ["""What is 20 * 2?__1__", "What is 100 * 2?__2__", "Who was the first Black President?__3__"," Who was the first president of the USA?__4__", 
"Where is Lebron James from (Hometown)?__5__", "1*1?__6__", "30*1000?__7__", "5 - 10?__8__",
"How many home alone movies are there?__9__","Who is the head coach of the Atlanta Falcoms?  ___10____"""]

hard_questions= ["How many wheels do normal cars have?___1___","What city is disney world located in Florida?___2___","What type of dog was Balto in the movie?___3___", 
"Did the Rock ever play college football (yes or no)?____4___","how many best man movies are there?____5____",
"What type of dog was lassie?____6____","100 + 100?___7___", "40+40?____8____", 
"What is the name of the team that Greg Popovich coaches?___9___", "What is the name of the football team in Atlanta?____10____"]


easy_answers= ["22", "atlanta", ".50", "atlanta hawks", "atlanta falcons" ,"five", "two", "eight" , "50", "athens"]
medium_answers= ["40", "200", "barack obama","george washington","akron ohio","1","30000","-5","4","dan quin"]
hard_answers= ["4", "orlando", "husky", "yes","2","collie","200","80","Spurs","Atlanta Falcons"]


#Lines 25 through 36 contains a dictionary which complies all my questions and answers.
#This makes everything easier to read  in my opinion 
question_guide = {
  "easy":{
    "answers":easy_answers,
    "questions": easy_questions
  },
  "medium":{
    "answers":medium_answers,
    "questions": medium_questions
  },
  "hard":{
    "answers":hard_answers,
    "questions": hard_questions
  }
}

def play_python_quiz():
  from datetime import datetime
  now = datetime.now()
  print("Today's Date is:")
  print('%s/%s/%s' % (now.month, now.day, now.year))
  print("")
  print('Hello welcome to my Quiz, what is your name?')
  usersname = input()
  print ("")
  print('It is nice to meet you, ' + usersname)
  select_level()

def quiz(questions, answers):
  score = 0
  num_answers = None
  for i, question in enumerate(questions):
    print("{}) {}".format(i+1, question))
    answer = input("Please fill in the blank with your answer below:\n")
    if answer.lower() == answers[i].lower():
      score += 1
  return score, len(answers)

questions = question_guide[select_level.lower()]["questions"]
answers = question_guide[select_level.lower()]["answers"]
score, num_answers = quiz(questions, answers)

print("Your score is {}/{}".format(score, num_answers))



def select_level(): #Inputs easy, medium or hard. Output, Relevant questions and answers.
    select_level = input("Select your difficulty level. easy | medium | hard")
    while select_level not in ["easy", "medium", "hard"]:
        select_level = input("Please follow instructions and try again")
    print ("game_questions[select_level]")
    return select_level


play_python_quiz()

Upvotes: 0

Views: 714

Answers (2)

toonarmycaptain
toonarmycaptain

Reputation: 2331

You try to call the variable 'select_level' before it appears in your code. The call to that variable needs to appear after it is defined. At the moment I believe it is not defined. You're also calling the function select_level() before it is defined.

You're returning the variable select_level but not assigning it to anything. This runs select_level:

def select_level(): #Inputs easy, medium or hard. Output, Relevant questions and answers.
    select_level = input("Select your difficulty level. easy | medium | hard")
    while select_level not in ["easy", "medium", "hard"]:
        select_level = input("Please follow instructions and try again")
    print ("game_questions[select_level]")
    return select_level
select_level() # runs function, but doesn't assign the returned value to a variable

This runs select_level and assigns the returned value (in this case the variable select_level to the variable select_level:

def select_level(): #Inputs easy, medium or hard. Output, Relevant questions and answers.
    select_level = input("Select your difficulty level. easy | medium | hard")
    while select_level not in ["easy", "medium", "hard"]:
        select_level = input("Please follow instructions and try again")
    print ("game_questions[select_level]")
    return select_level

select_level = select_level() #assigned return value to variable so you can use it later.

You need to move that function (and assigning the returned value to a variable) above where that variable is stored.

My suggestion is that it would be clearer to not have a variable and function named the same thing. I had originally assigned by selected_level = select_level() - but then the .lower thought you were calling the function select_level rather than a variable.

Upvotes: 0

Aaron Lael
Aaron Lael

Reputation: 188

This is calling select_level before it is defined (line 63):

questions = question_guide[select_level.lower()]["questions"]

So is this (line 64):

answers = question_guide[select_level.lower()]["answers"]

You define select_level on line 70.

Upvotes: 1

Related Questions