1

First I will post my code. I feel that there's a simple answer that I'm somehow overlooking.

questionsList=[]
answersList=[]
def retrieveQuestions():
    f=open('Questions.txt')
    questionsList=f.read().splitlines()
    for row in f:
        questionsList.append(row)

def retrieveAnswers():
    with open('Answers.txt') as j:
        answersList=list(j.read().splitlines())

retrieveQuestions()
retrieveAnswers()
print(questionsList)
print(answersList)

When I run the program, I get output of [] [] I looked up multiple methods of file path, multiple ways to read and .splitlines(), and I still don't know what's wrong.

2

2 Answers 2

1

Currently, your methods only create and populate lists but nothing else -no screen output or return. Simply add a return to your functions and assign values to a variable and print or print inside each called function. And you do not need the global variable assignment at top as you define variables inside the methods.

Return and Assign

def retrieveQuestions():
    f=open('1222_2016_2016-2017.csv')
    questionsList=f.read().splitlines()
    for row in f:
        questionsList.append(row)
    f.close()

    return questionsList

def retrieveAnswers():
    with open('ODDFiles.csv') as j:
        answersList=list(j.read().splitlines())

    return answersList

questionsList = retrieveQuestions()
answersList = retrieveAnswers()

print(questionsList)
print(answersList)

Print Inside

def retrieveQuestions():
    f=open('1222_2016_2016-2017.csv')
    questionsList=f.read().splitlines()
    for row in f:
        questionsList.append(row)
    print(questionsList)

def retrieveAnswers():
    with open('ODDFiles.csv') as j:
        answersList=list(j.read().splitlines())
    print(answersList)

retrieveQuestions()
retrieveAnswers()
Sign up to request clarification or add additional context in comments.

4 Comments

This works. Although why do I have to put questionsList=retrieveQuestions() ? Shouldn't the retrieveQuestions method by itself assign input to questionsList? That's what it's doing inside the function correct?
Read up on object-oriented programming. Not just Python, but in most languages, anything inside a function is local to that function and does not exist outside unless you return a value.
However, I used global lists inside the function. Why didn't that work?
As mentioned, you re-assigned inside function so global variables were not not needed, plus you did not return anything, also mentioned. You might be needing to use global. See here.
0

Try it:

questionsList=[]
answersList=[]
def retrieveQuestions():
    f=open('Questions.txt', 'r')
    questionsList = f.readlines().splitlines()

def retrieveAnswers():
    with open('Answers.txt', 'r') as j:
        answersList = j.readlines().splitlines()

retrieveQuestions()
retrieveAnswers()
print(questionsList)
print(answersList)

Sugestion:

def read_file(filename):
    return open(filename, 'r').readlines().splitlines()

questionsList = read_file('Questions.txt')
answersList = read_file('Answers.txt')

print(questionsList)
print(answersList)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.