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?
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:Then when you call
function2, do as Ade described: