Reputation:
import random
class Trivia(object):
points = 0
def __init__(self, mult_tf, question, options, correct, reason):
self.mult_tf = mult_tf
self.question = question
self.options = options
self.correct = correct
self.reason = reason
mult_tf, question, options, correct, reason = block
def next_line(the_file):
line = the_file.readline()
line = line.replace('/', '\n')
return line
def questions(self, block):
file_name = open('Trivia_Questions.txt', 'r')
while True:
mult_tf = next_line(file_name)
question = next_line(file_name)
options = []
if mult_tf == 'Multiple Choice':
for i in range(4):
options.append(next_line(file_name))
else:
for i in range(2):
options.append(next_line(file_name))
correct = next_line(file_name)
reason = next_line(file_name)
mult_tf, question, options, correct, reason = block
file_name.close()
def rand_quest(self):
for block in questions():
questions.shuffle()
return questions
def show(self):
while True:
print mult_tf
print question
print options
guess = raw_input('\nWhich do you choose? ')
if guess == correct:
print '\nCorrect!'
points += 10
else:
print '\nIncorrect!'
print reason
print '\nYou completed the game!'
print '\nYou got', points, 'points!'
Trivia.show()
raw_input('\n\nPress enter to exit')
I posted a question a few hours ago on the same question. I believe I have gotten a little further in my search for an answer. The code above is supposed to be on a trivia game with multiple choice and true/false questions. I believe I am close to the solution I just cannot get it to run properly. I constantly get this error:
Trivia.show()
TypeError: unbound method show() must be called with Trivia instance as first argument (got nothing instead)
Not quite sure what the arguement should be, I thought that the show method would print what I needed it to print.
Upvotes: 0
Views: 365
Reputation: 121966
show()
is an instance method, so to call it you need to create an instance of the Trivia
class, e.g.:
trivia = Trivia(mult_tf, question, options, correct, reason)
trivia.show()
More broadly, though, your class doesn't make any sense. A few examples:
__init__
won't work, because in the last line:
mult_tf, question, options, correct, reason = block
which means "unpack the five items in block
into these other five variables", the name block
isn't defined, so you will get a NameError
. Therefore you can never create a class instance and nothing else will run.
next_line
seems fine, except you don't have a self
argument, but it isn't really a class or instance method (it doesn't use any attributes of the class or instance) so probably doesn't belong in the class anyway.
questions
is a bit odd; it gets all these values from the file, then the method ends without actually doing anything with them, having overwritten their values with whatever is in block
. Nothing gets returned or added to the instance, so it may as well never have run.
rand_quest
also has name errors, as questions
isn't defined (nor is it an argument). You could call self.questions()
, except that doesn't really do or return anything, as discussed. Functions don't have a shuffle()
method unless you define it, and even if you got the syntax right (random.shuffle(questions)
) you can't shuffle a function - what would that even mean?
In show
, there is nothing to end the while True
loop, so this would just run forever, except for the fact that you have a whole load of NameError
s there too (e.g. mult_tf
doesn't exist in that scope, although self.mult_tf
does.
I think you need to split this into three things:
Trivia
class, where each instance is one question. This has an __init__
method (takes the attributes, returns nothing), a show
method (takes no arguments, returns a string) and a check_answer
method (takes a string, returns a boolean).import_file
function, which takes a filename and returns a list of Trivia
instances based on the contents of the file.import_file
then random.shuffle
s them and asks them.Upvotes: 2