Python测验有错误消息说没有定义吗?

时间:2017-11-29 20:05:08

标签: python python-3.6

以下代码的问题是,当我向底部定义问题时,我不断收到此错误:

此外,如果有任何方法可以缩短此代码,我会非常感谢整体帮助,我相信我已尽可能缩短了这一点,但由于我是python的新手,我很乐意接受任何反馈,谢谢。

追踪(最近一次通话):   文件“python”,第63行,in NameError:未定义名称“select_level”

#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()

2 个答案:

答案 0 :(得分:1)

这是在定义之前调用select_level(第63行):

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

这是(第64行):

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

您可以在第70行定义select_level。

答案 1 :(得分:0)

您尝试在代码中出现之前调用变量'select_level'。对该变量的调用需要在定义之后出现。目前我认为它没有定义。您还在定义函数select_level()之前调用它。

您将返回变量select_level,但不会将其分配给任何内容。 这会运行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

这会运行select_level并将返回的值(在本例中为变量select_level)分配给变量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.

您需要在存储该变量的位置上方移动该函数(并将返回的值赋值给变量)。

我的建议是,没有变量和函数命名相同的东西会更清楚。我最初是由selected_level = select_level()分配的 - 但后来.lower认为您正在调用函数select_level而不是变量。