类的“Python NameError:name未定义”

时间:2016-05-01 01:40:46

标签: python

我在Python代码中完全定义Questions类时遇到了困难。我玩过它,但没有运气。

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 

1 个答案:

答案 0 :(得分: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