2016-05-01 151 views
-2

我很難在Python代碼中完全定義Questions類。我玩過它,但沒有運氣。「Python NameError:name is not defined」for

import sys 
import random 


class Question(): 
    def __init__(self, ques, pa1, pa2, pa3, pa4, correct): 
     self._q = ques 
     self._pa1 = pa1 
     self._pa2 = pa2 
     self._pa3 = pa3 
     self._pa4 = pa4 
     self._correct = correct 

    def get_ques(self): 
     return self._q 
    def get_pa1(self): 
     return self._pa1 
    def get_pa2(self): 
     return self._pa2 
    def get_pa3(self): 
     return self._pa3 
    def get_pa4(self): 
     return self._pa4 
    def get_correct(self): 
     return self._correct 


    def main(): 
     lst_ques = [ 
      Question("What is our nation's capital", 3, ["Texas", "Virginia", "Washington, D.C.", "New York"]), 
      Question("How many burrows make up New York city", 4, ["two", "four", "three", "five"]), 
      Question("In what month does the leap year occur", 1, ["February", "July", "December", "October"]), 
      Question("What state do the Cowboys football team play for", 2, ["New York", "Texas", "California", "Utah"]), 
      Question("What's the symbol to Iron", 1 ,["Fe", "Ie", "Ir", "In"]), 
      Question("Where is Notre Dame", 4 ,["Michigan", "Japan", "Ireland", "France"]), 
      Question("About how many billion years old is the sun", 3 ,["1", "4", "5", "2"]), 
      Question("What's the most malleable metal", 2 ,["iron", "gold", "aluminum", "steel"]), 
      Question("What is short for binary digit", 2 ,["bd", "bit", "bin", "digit"]), 
      Question("What is the Indiana state bird", 4 ,["robin", "eagle", "finch", "cardinal"]), 
       ] 
     print('Player #1, please begin.') 
     i = 1 
     ca1 = 1 
     ques_attempted = [] 
     while i<=5: 
      number = random.radiant(0,9) 
      if number not in ques_attempted: 
       print('Question',i) 
       print(lst_ques[number].get_ques()) 
       print(lst_ques[number].get_pa1()) 
       print(lst_ques[number].get_pa2()) 
       print(lst_ques[number].get_pa3()) 
       print(lst_ques[number].get_pa4()) 
       answer = input('Enter in correct answer: ') 
       if ques[number].get_correct()==int(ans): 
        print('Correct!') 
        ca1+=1 
       else: 
        print('Incorrect') 
        ques_attempted.append(number) 
        i+= 1 
        print('Player #2, it is now your turn, please begin.') 
        i = 1 
        ca2 = 1 
        ques_attempted = [] 
       while i<=5: 
        number = random.radiant(0,9) 
        if number not in ques_attempted: 
         print('Question',i) 
       print(lst_ques[number].get_ques()) 
       print(lst_ques[number].get_pa1()) 
       print(lst_ques[number].get_pa2()) 
       print(lst_ques[number].get_pa3()) 
       print(lst_ques[number].get_pa4()) 
       answer = input('Enter in correct answer: ') 
       if ques[number].get_correct()==int(ans): 
        print("Correct!") 
        i = 1 
        ca2+=1 
     else: 
      print("Incorrect") 
      ques_attempted.append(number) 
      print('The final scores are: ') 
      print('Player #1: ', ca1) 
      print('Player #2: ', ca2) 
     if ca1 > ca2: 
      print('Player #1 is the winner.') 
     elif ca2 > ca1: 
      print('Player #2 is the winner.') 
     else: 
      print('The final scores are the same, the game is a tie.') 

    main() 

它拋出以下異常:

Traceback (most recent call last): 
    File "C:\Users\Leonard\Documents\Lindsay's Files\Python_TEST.py", line 5, in <module> 
    class Question(): 
    File "C:\Users\Leonard\Documents\Lindsay's Files\Python_TEST.py", line 94, in Question 
    main() 
    File "C:\Users\Leonard\Documents\Lindsay's Files\Python_TEST.py", line 30, in main 
    Question("What is our nation's capital", 3, ["Texas", "Virginia", "Washington, D.C.", "New York"]), 
NameError: name 'Question' is not defined 
+0

拋出的異常在哪裏? –

+1

@DawidFerenczy:是不是很明顯:) - 在'代碼在這裏'代碼行! – usr2564301

+1

'number = random.radiant(0,9)'應該是'number = random.randint(0,9)' – Natecat

回答

2

很簡單。您已將main定義爲Question類的方法,但我想它應該只是一個函數,因此將其向左移動一個縮進級別並將其固定。

因此,它應該是這樣的:

class Question: 
    def __init__(): 
    # other methods 

def main(): 
    # code 

main() 

說明:在代碼中,你定義main作爲Question類的方法,你也執行它的類裏面它的定義之前,這就是爲什麼你是否得到NameError例外'Question' is not defined

class Question(): 
    def main(): # here you define main as Question's method 
     # some code 

    main() # here you're executing the main method before Question's definition is finished 
+0

好的,我做到了。現在我得到錯誤... TypeError:__init __()缺少3個必需的位置參數:'pa3','pa4'和'correct'。 –

+0

那個異常的哪個部分你不明白?我認爲這很明顯。你用6個參數定義了Question的構造函數,但是你只用3個參數來調用它。 –

+0

@LinzG事情是,你的班級的定義和你怎麼稱呼它不馬赫,與目前的定義,我認爲你要把它稱爲問題(「什麼是符號鐵」,「鐵」, 「Ie」,「Ir」,「In」,1)' – Copperfield

相關問題