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.

Nicholas A. Randall
  • 421
  • 1
  • 5
  • 13

2 Answers2

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()
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • 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? – Nicholas A. Randall Aug 23 '17 at 23:22
  • 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. – Parfait Aug 24 '17 at 02:39
  • However, I used global lists inside the function. Why didn't that work? – Nicholas A. Randall Aug 24 '17 at 02:47
  • 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](https://stackoverflow.com/q/10588317/1422451). – Parfait Aug 24 '17 at 02:56
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)
Abe
  • 1,357
  • 13
  • 31