Reputation: 379
I have a function that takes 2 returned variables from another function to write them with a little bit more padding text to an external text file.
The code that I am using to write to the text file is below but it is being called automatically at runtime with the if
statement at the bottom of the program
def function1()
# Some code
def function2()
# Some code
return startNode
return endNode
def function3(var)
# Some code
return differentVariable
def createFile(startNode, endNode):
outputFile = open('filename.txt','w') # Create and open text file
start = chr(startNode +65) # Converts the int to its respective Char
end = chr(endNode +65) # Converts the int to its respective Char
outputFile.write('Route Start Node: ' + start + '\n') # e.g Route Start Node: B
outputFile.write('Route end node: ' + end + '\n') # e.g Route End Node: F
outputFile.write('Route: ' + Path + '\n') # e.g Path: B,A,D,F
outputFile.write('Total distance: ' + Distance) # e.g Total Distance: 4
outputFile.close # Close file
if __name__ == "__main__":
function1()
function3(function2())
createFile( # Don't Know What Goes Here )
At the moment I am only able to run this function by manually calling it and entering createFile(1,5)
where the 1 is the startNode value and the 5 is the endNode value
But if I am to put createFile(startNode, endNode)
in the if
statement to call the function, it tells me NameError: name 'startNode' is not defined
and then it would obviously give me the same error for endNode too
How do I call that function properly without having to enter the values manually because they can change depending on what the startNode and endNode values are at the start of the program?
Upvotes: 1
Views: 136
Reputation: 5945
You can't return two variables as you've done in your example of function2
. Execution will exit the function at the first return value, and never make it to the second. Instead, you need to return a tuple:
def function2():
# set the values of startNode and endNode here
# e.g.
startNode = 1
endNode = 5
return (startNode, endNode)
Then when you call function2
, do as Ade described:
if __name__ == "__main__":
startNode, endNode = function2()
createFile(startNode, endNode)
Upvotes: 2
Reputation: 387557
But if I am to put
createFile(startNode, endNode)
in the if statement to call the function, it tells meNameError: name 'startNode' is not defined
Obviously, because all that is being looked at at that point is this:
if __name__ == "__main__":
function1()
function3(function2())
createFile(startNode, endNode)
So if you want to make that last call succeed, you need to define those variables somewhere.
I am guessing that those function calls before are used to initialize the values in some way. You probably “set” startNode
and endNode
in them some how, but that won’t work, as variables inside functions are usually local. So what you want to do is return the values from the functions and save them instead:
if __name__ == "__main__":
startNode = function1()
endNode = function3(function2())
createFile(startNode, endNode)
And the functions should look like this:
def function1 ():
# some computations
return startNode
def function3 (param):
# more computations
return endNode
Upvotes: 1
Reputation: 2362
You should assign the startNode
and endNode
to some values.
If they are return by the 2 previous functions:
if __name__ == "__main__":
startNode = function1()
endNode = function3(function2())
createFile(startNode, endNode)
Upvotes: 0