Ich versuche, ein Quiz für mein Projekt zu machen, und ich bekomme diesen Fehler: AttributeError: 'module' object has no attribute 'question'
. Ich verstehe nicht, warum es meine Klasse für ein Attribut hält.Das Objekt 'Modul' hat kein Attribut 'quetion'. Klassenname wird als Attribut betrachtet?
questionbf.py ist, wo ich die Binärdatei gemacht habe.
quizbf.py ist, wo ich versuche, das Quiz richtig zu machen.
Ich habe keine Erfahrung in Python, so dass überhaupt etwas hilfreich wäre. Vielen Dank.
questionbf.py
import pickle
class question:
def __init__(self,a,b,c):
self.q=a
self.an=b
self.o=c
f1=open("Question.DAT","wb")
n=input("Enter no. of Questions ")
for i in range(n):
a=raw_input("Enter Question ")
b=raw_input("Enter Answer ")
c=raw_input("Enter Options ")
s=question(a,b,c)
pickle.dump(s,f1)
f1.close()
quizbf.py
import pickle
print '''Welcome to the revision quiz.'''
print
score=0
w=0
c=0
f1=open("Question.DAT","rb")
try:
while True:
s=pickle.load(f1)
print s.q
print s.o
guess=input("Enter Choice ")
if guess==s.a:
print "Correct!!"
print
score=score+1
c=c+1
elif guess=="exit" or guess=="Exit":
break
else:
w=w+1
print "Incorrect. Better luck next time!!"
print
except EOFError:
f1.close()
print s
print w
Fehler:
Traceback (most recent call last):
File "C:\Users\RUBY\Desktop\questionbf.py", line 32, in <module>
s=pickle.load(f1)
File "C:\Python27\lib\pickle.py", line 1378, in load
return Unpickler(file).load()
File "C:\Python27\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python27\lib\pickle.py", line 1069, in load_inst
klass = self.find_class(module, name)
File "C:\Python27\lib\pickle.py", line 1126, in find_class
klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'question'
Ich weiß nicht Python, aber für mich klingt das wie es kann die Objekte nicht entpacken, weil die Klasse "Frage" nicht definiert ist. – melpomene
1) Klassen sollten in Python groß geschrieben werden. 2) Haben Sie versucht, eine Frage zu importieren? Mit anderen Worten, Sie müssen die 'question'-Klasse importieren, damit das Modul davon erfährt. –
@ cricket_007 Sie müssen möglicherweise eine separate' question.py'-Datei erstellen, die nur die Klassendefinition enthält (die Sie dann importieren) in beiden Skripten). Wenn nicht, vermute ich, dass Python das gesamte 'questionbf'-Skript erneut ausführen wird. – melpomene