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:
- Why are there so many
''after the first two strings in the test_questions list? - Why are the strings that are meant to populate the test_answers list being appended to 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