2016-05-01 4 views
-2

Ich habe Schwierigkeiten, die Klasse Fragen in meinem Python-Code vollständig zu definieren. Ich habe damit herumgespielt, hatte aber kein Glück."Python NameError: Name ist nicht definiert" für Klasse

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

Es wirft die folgende Ausnahme:

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

Wo wird diese Ausnahme ausgelöst? –

+1

@DawidFerenczy: ist es nicht offensichtlich :) - an der Zeile "Code" am Ende! – usr2564301

+1

'number = random.radiant (0,9)' sollte 'number = random.randint (0,9)' – Natecat

Antwort

2

Es einfach. Sie haben die main als Question Klasse 'Methode definiert, aber ich denke, es sollte nur eine Funktion sein, so verschieben Sie es um eine Einrückungsebene nach links und es ist behoben.

So sollte es so aussehen:

class Question: 
    def __init__(): 
    # other methods 

def main(): 
    # code 

main() 

Erläuterung: in Ihrem Code Sie die main als Question Klasse Methode definieren und Sie es auch innerhalb der Klasse ausführen, bevor sie definiert ist, das ist, warum erhalten Sie die NameError Ausnahme '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

Okay, das habe ich getan. Jetzt bekomme ich den Fehler ... TypeError: __init __() fehlt 3 erforderliche positionale Argumente: 'pa3', 'pa4' und 'richtig'. –

+0

Und welchen Teil dieser Ausnahme verstehst du nicht? Ich denke, es ist ziemlich klar und offensichtlich. Sie haben den Konstruktor von Question mit 6 Argumenten definiert, aber Sie rufen ihn nur mit 3 Argumenten auf. –

+0

@LinzG die Sache ist, dass die Definition Ihrer Klasse und wie Sie es nennen mach es nicht, mit der aktuellen Definition denke ich, dass Sie es als 'Frage nennen wollen (" Was ist das Symbol für Eisen "," Fe ", "Ich", "Ir", "In", 1) ' – Copperfield

Verwandte Themen