What am i missing regarding return?

0

Issue

I am new to programming, and I stumbled over the same phenomenon several times. Maybe I am stupid, but I don’t understand why sometimes I can write working def functions with return statements, and "inheritance"(?) of the values works just fine, and sometimes not.

Here is the recent example where it doesn’t work, and i don’t know why:

import sys

file_1 = sys.argv[1]

file_2 = sys.argv[2]

text_1 = []

text_2 = []

def reading(file_1, file_2):

for line_1 in file_1:
    line_2 = file_2.readline()
    text_1.append(line_1)
    text_2.append(line_2)
return text_1, text_2

file_1 = open(file_1, ‘r’)

file_2 = open(file_2, ‘r’)

text_1, text_2 = reading(file_1, file_2)

print(text_1)

print(text_2)

file_1.close()

file_2.close()

Solution

You need to assign the return value of your reading function, like so line_1, line_2 = reading(file_1, file_2). line_1 and line_2 are private to your reading function.

Answered By – vinzenz

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More