2017-11-29 9 views
0

下のコードで問題が発生した場合は、このエラーが発生し続けます。Pythonクイズで、定義されていないというエラーメッセージが表示される

また、このコードを短くする方法がある場合は、できるだけこれを短くしましたが、私はPythonには新しいので、私は喜んでフィードバックを受け入れることに感謝します。

トレースバック(最新の呼び出しの最後): NameErrorで ファイル "のpython"、行63:名 '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() 
+0

エラーは '' select_level 'が定義されていません – aug2uag

答えて

1

が定義されていない、それが定義される前に、これは(select_levelを呼び出しています63行目):

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

だから、これは(行64)です。

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

70行目でselect_levelを定義します。

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を呼び出していると考えました。

関連する問題