2016-05-01 152 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

相关问题