1

I tried to code a program but ran into problem with the return statement.

The following lines of code raised an error message saying that the variable names is undefined.

However, I did use the return statement to return names and pass it to the main function:

def main(names):
    names_in()
    print(names)

# import the record, put it into a list,  pass that list to main().
def names_in():
    infile = open('names.txt','r')
    names = infile.readlines()

    infile.close()

    index = 0
    while index < len(names):
        names[index] = names[index].rstrip('\n')
        index += 1
    return names

main(names)

I've written another program before that did the same thing and everything worked fine, so I'm not sure what's wrong here?

5
  • Well, what do you want to do with def main(names):? Does it really need an argument here? And maybe you mean print(names_in()). Commented Oct 30, 2015 at 0:42
  • Off-topic: names_in makes me cringe. It could simplify to: with open('names.txt', 'r') as infile: return [name.rstrip('\n') for name in infile] and would run faster, as well as more obviously and idiomatically. Commented Oct 30, 2015 at 0:45
  • @ShadowRanger: I did that because the homework requires us to have that function in our program. Commented Oct 30, 2015 at 1:02
  • I'm not against having the function. It's the construction of it (emulating a C-like for loop, slurping a file then modifying the lines after the fact, explicit close instead of allowing with to do the work reliably/safely). Literally the whole function could be rewritten as two idiomatic lines. Commented Oct 30, 2015 at 3:36
  • @ShadowRanger: thank you for your correction, I'm new to programming languages so these tips are very helpful for me. Commented Oct 30, 2015 at 14:43

2 Answers 2

5

Your problem is here:

main(names)

You are calling your main function with a variable name that hasn't been defined yet. The error you're getting should display a line number that points to this line: that will validate this is where the problem is.

# 'names' is not defined in this scope
def main(names):
    # Some code
    # 'names' is a variable that is 'in scope' here because it's passed as an 'argument'

# 'names' is not defined in this scope either

# import the record, put it into a list,  pass that list to main().
def names_in():
    infile = open('names.txt','r')
    names = infile.readlines()  # defines 'names' for this scope

    # more code
    return names  # because 'names' is defined it can return it

# 'names' is no longer defined in this scope
main(names)  # This fails because when the compiler tries to resolve 'names' it can't find anything

Based on what you're apparently trying to do, you need to modify your main function like so:

def main():
    print(names_in())

And call it like so:

main()

Note, too, you seem to have some confusion over how code is executed. If I write:

def a():
    print("a")

def b():
    print("b")

print("c")
b()
a()

It gets translated to something equivalent to:

print("c")
print("b")
print("a")

The def statements get copy-pasted, effectively, into where they are called.

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

2 Comments

Thank you for your help. Just one more follow-up question. The return statement here only returns whatever stored in variable "names" back to the function names_in() after the function's done with all of the steps right?
That is correct. But it just returns the value, not the name.
1

Your main function should not take in any parameter. Instead, it should use the return value from names_in:

def main():
    names = names_in()
    print(names)

Later in your code:

main()

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.