Reputation:
import random
players = 0
player1 = 0
player2 = 0
def open_file(file_name, mode):
try:
the_file = open(file_name, mode)
except(IOError), e:
print 'Cannot open file', file_name + '. Try moving its location.'
raw_input('\nPress enter to exit. ')
sys.exit()
else:
return the_file
def next_line(the_file):
line = the_file.readline()
line = line.replace('/', '\n')
return line
def main():
file = open_file('Trivia_Questions.txt', 'r')
questions = file.read().split('\n\n')
file.close()
random.shuffle(questions)
for question in questions.splitlines():
if next_line(question) == 'Multiple Choice':
subject, question, answer1, answer2, answer3, answer4, reason, empty = map(question)
print subject
print question
print '1 -', answer1
print '1 -', answer1
print '1 -', answer1
print '1 -', answer1
print reason
subject, question, answer1, answer2, answer3, answer4, reason, empty = next_block(file)
else:
subject, question, answer1, answer2, reason, empty = map(question)
print subject
print question
print '1 -', answer1
print '2 -', answer2
print reason
print empty
subject, question, answer1, answer2, reason, empty = next_block(file)
main()
I have been looking up on how to go about doing this, and have no idea what to do. When I attempt to run this code i get
AttributeError: 'list' object has no attribute 'splitlines'
My txt file is setup like mult choice question answer1 answer2 answer3 answer4 correct answer# reasonwhy
true/false question t f correct t or f reasonwhy
repeat 12x
ive been searching online for 2 days before i decided to ask so any assistance would be much appreciated. I need to correctly have a trivia game randomly select a question, whether its true/false for mult choice, not use the question again.
Upvotes: 0
Views: 993
Reputation: 1121486
You already split your file into a list:
questions = file.read().split('\n\n')
so questions
is a list here. You cannot then try to split questions
into a new list here:
for question in questions.splitlines():
If you wanted to split each individual question into separate lines, do so in the loop:
for question in questions:
for questionline in question.splitlines()
Upvotes: 2