Reputation: 37
I am trying to open two text files, read them, and place contents in two separate temporary files to call upon them later. I have the following two problems:
''
after the first two strings in the test_questions list?The code looks like this:
from os import read
# vars to hold questions and answers from question bank and answer bank in format ready for quiz iterations
test_questions = []
test_answers = []
# placing questions in temp list called test_questions
def read_quest():
bank = open("test_question_bank.txt", "r+")
bank.seek(0)
file_read_q = bank.readlines()
start = 0
stop = 5
for i in file_read_q:
temp_q = "".join(file_read_q[start:stop])
print(test_questions.append(temp_q))
start += 5
stop += 5
bank.close()
# placing answers in temp list called test_answers
def read_answers():
ans = open("test_correct_ans.txt", "r+")
file_read_a = ans.readlines()
ans.seek(0)
for i in file_read_a:
i = i.strip("\n")
print(test_questions.append(i))
ans.close()
# adding questions
def add_quest():
bank = open("test_question_bank.txt", "a")
correct_ans = open("test_correct_ans.txt", "a")
prompt = input("Please write question: ")
ansa = input("Write answer for choice a): ")
ansb = input("Write answer for choice b): ")
ansc = input("Write answer for choice c): ")
correct_opt = input("Which option is the correct answer? ")
temp_quest = prompt + "\n(a) " + ansa + "\n(b) " + ansb + "\n(c) " + ansc + "\n\n"
temp_quest2 = "".join(temp_quest)
print(bank.write(temp_quest2))
print(correct_ans.write(correct_opt + "\n"))
bank.close()
correct_ans.close()
read_quest()
read_answers()
print(test_questions)
print(test_answers)
#add_quest()
The output looks like this:
None
None
None
None
None
None
None
None
None
None
None
None
['Where is North?\n(a) s\n(b) e\n(c) n\n\n', 'Where is South?\n(a) s\n(b) e\n(c) n\n\n', '', '', '', '', '', '', '', '', 'c', 'a']
[]
The content of test_question_bank.txt
looks like this:
Where is North?
(a) s
(b) e
(c) n
Where is South?
(a) s
(b) e
(c) n
The content of my test_correct_answers.txt
looks like this:
c
a
Upvotes: 4
Views: 397
Reputation: 37
Wow! Thanks for all your input. I have certainly learnt a lot here. I feel a little embarrassed as the answer to my 2nd question was simple: I was appending to the wrong file!
As for the first question, several helpful solutions - thanks again - and you taught me that the problem was how the file was being iterated. I thought to do the following which also seemed to work. I simply added an if loop to test if the start index had exceed the index of the items it was iterating over.
if start < len(file_read_q):
Maybe you can feedback to me if there will be potential problems with this solution.
Thanks again to all.
def read_quest():
bank = open("test_question_bank.txt", "r+")
bank.seek(0)
file_read_q = bank.readlines()
start = 0
stop = 5
for i in file_read_q:
if start < len(file_read_q):
temp_q = "".join(file_read_q[start:stop])
test_questions.append(temp_q)
start += 5
stop += 5
else:
break
bank.close()
Upvotes: 0
Reputation: 1684
As you are new, there are several recommendations:
test_questions
, test_answers
), it's hard to debug and control your programs.read_questions
is better than read_ques
with open('..')
syntax.read_quest
, instead of stepping 5 lines over the questions, try to extract the questions by a string pattern.Check code below:
def read_questions():
with open('test_question_bank.txt') as f:
valid_lines = [x.strip() for x in f.readlines() if x.strip()]
valid_questions = [x for x in valid_lines if not x.startswith('(')]
return valid_questions
def read_answers():
with open('test_correct_answers.txt') as f:
valid_answers = [x.strip() for x in f.readlines() if x.strip()]
return valid_answers
questions = read_questions()
valid_answers = read_answers()
print(questions)
print(valid_answers)
Enjoy Python.
Upvotes: 1
Reputation:
Why do so many
''
appear? This is because the.readlines()
reads each line as as a separate element. So your list looks something like['Where is North?\n', '(a) s\n', '(b) e\n', '(c) n\n', '\n', 'Where is South?\n', '(a) s\n', '(b) e\n', '(c) n']
.
Now, you are doing the following:
file_read_q[0:5]
file_read_q[5:10]
So, in theory, you cover the whole list in 2 slices as you are fetching 5 elements at a time. But the for
loop is iterating over the elements of the list and you are fetching slices whose indexes don't even exist in the list. So all you get is a blank list []
which, when joined, makes ''
But if the index is out of the list, why is there not an error raised?
For that, you can refer here
def read_quest():
bank = open("test_question_bank", "r+")
#bank.seek(0)
file_read_q = bank.readlines()
start = 0
stop = 5
for i in file_read_q:
temp_q = "".join(file_read_q[start:stop]).strip("\n")
if temp_q:
test_questions.append(temp_q)
start += 5
stop += 5
bank.close()
You get an empty test_answers
because are never actually appending an element to the test_answers
instead, you are doing test_questions.append(...)
. So do test_answers.append(...)
def read_answers():
ans = open("test_correct_ans.txt", "r+")
file_read_a = ans.readlines()
ans.seek(0)
for i in file_read_a:
i = i.strip("\n")
test_answers.append(i)
ans.close()
Upvotes: 1
Reputation: 43
You have a lot of blank lines that are in your files (probably at the end, where you don't notice them). You can clean you data by running the following to get rid of any blank lines:
# For each question
# If it is not a blank line
# Add it to the new list
test_questions = [question for question in test_questions if question]
Upvotes: 4