1

I am trying to pass multiple lines of lists from one function to another but I cannot figure out how it is done. Here is the code I have so far:

def readfile():
'''Read a text file; return a string holding the text'''
    f = open("numbers.txt", 'r')
    line = f.readlines()             
    f.close()           
    return line  

def dataConversion(lines):

    lst = []
    for element in lines:
        lst = element.strip()
        lst = map(int, lst)
        print lst        
return lst

def evenNumberList(lsts):
    print lsts

def main():    

    lines = readfile()   
    lsts = dataConversion(lines)
    doubledList = evenNumberList(lsts)

main()

The output of the dataConversion(lines) function is:

[4, 3, 8, 8, 5, 7, 6, 0, 1, 8, 4, 0, 2, 6, 2, 6]
[4, 3, 8, 8, 5, 7, 6, 0, 1, 8, 4, 1, 0, 7, 0, 7]
[4, 0, 1, 2, 8, 8, 8, 8, 8, 8, 8, 8, 1, 8, 8, 1]
[4, 5, 5, 2, 7, 2, 0, 4, 1, 2, 3, 4, 5, 6, 7, 7]
[4, 5, 3, 9, 9, 9, 2, 0, 4, 3, 4, 9, 1, 5, 6, 2]
[4, 9, 9, 2, 7, 3, 9, 8, 7, 1, 6, 0, 0]
[4, 9, 9, 2, 7, 3, 9, 8, 7, 0, 0, 1, 7]
[8, 0, 8, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 5, 8, 8, 3, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 4, 9, 1, 9, 4, 6, 9, 1, 5, 4, 4, 4, 9, 2, 3]
[5, 4, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 8]
[3, 7, 8, 2, 8, 2, 2, 4, 6, 3, 1, 0, 0, 0, 5]
[3, 7, 1, 4, 4, 9, 6, 3, 5, 3, 9, 8, 4, 3, 1]
[3, 7, 1, 4, 4, 9, 6, 3, 5, 3, 9, 8, 4, 3, 1]
[3, 7, 8, 7, 3, 4, 4, 9, 3, 6, 7, 1, 0, 0, 0]
[3, 7, 8, 7, 3, 4, 4, 9, 3, 6, 7, 1, 0, 0, 1]
[6, 0, 4, 1, 2, 7, 3, 9, 9, 0, 1, 3, 9, 4, 2, 4]
[6, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 7]
[6, 0, 1, 1, 0, 0, 0, 9, 9, 0, 1, 3, 9, 4, 2, 4]

While the input to the evenNumberList(lsts) function is:

[6, 0, 1, 1, 0, 0, 0, 9, 9, 0, 1, 3, 9, 4, 2, 4]

How do I make them match? I need all of the lines of code in the evenNumberList(lsts) function not just the one line. My professor told me I need to call the function from within a loop but I haven't been able to figure out how to do that.

2 Answers 2

1

I'd write your code like this:

def dataConversion(lines):
  temp = []

  for element in lines:
    converted = map(int, element.strip())
    temp.append(converted)

  return temp

def evenNumberList(lsts):
  return lsts

if __name__ == '__main__':
  lines = open("numbers.txt", 'r').readlines()   
  lsts = dataConversion(lines)
  doubledList = evenNumberList(lsts)

  print lsts
  print doubledList

The main problem you had was using return properly. print is not return. Your evenNumberList() function returned absolutely nothing.

Also, in dataConversion(), you were using map() on an empty list, which produced no actual output either.

My rule of thumb: don't use print statements inside of functions like that. Take them out of the function and into the actual program, as errors are easier to catch that way.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. I realize that print is not return. The only reason print was there was so that I could see what my output was. I forgot to remove it when I posted the code. Also, my evenNumberList() function was not supposed to return anything yet. I am still trying to get the correct data into it. Once I get the right data into it I will do what I have to do to get the output I need. I am not far along enough in the semester to know what name and main are so I can't use those but I now know where I was going wrong. Once again thank you.
The stuff inside if __name__ == '__main__' is run when the Python file is run.
0

In dataConversion() you are rebinding your lst variable every time through the loop, so you only return the data for the result for the very last iteration.

Your main loop indicates that what you expect to get from this function is lsts, or multiple lists, but you are only returning one.

To get you started, in dataConversion() you should create a variable that will be the return value, maybe call it lsts or list_of_lists, then in the for loop do lsts.append(lst) where lst is the list of integers that you currently create from element.

1 Comment

Thank you very much for your help. I think I can figure things out now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.