![]() |
I'm having trouble printing a return value in my main function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I'm having trouble printing a return value in my main function (/thread-9141.html) |
I'm having trouble printing a return value in my main function - RedSkeleton007 - Mar-23-2018 I'm missing something very basic here. The following code: #!/usr/bin/env python3 #QuickTest.py def summ(numThree,numFour): result = numThree + numFour return result def main(): numThree = 3 numFour = 4 summ(numThree,numFour) print(result) main()Gives this error: I was expecting it to print 7. What's wrong?
RE: I'm having trouble printing a return value in my main function - wavic - Mar-23-2018 !/usr/bin/env python3 #QuickTest.py def summ(numThree,numFour): result = numThree + numFour return result def main(): numThree = 3 numFour = 4 result = summ(numThree,numFour) print(result) main()summ returns a value but it is not assigned to anything. The 'result' variable is local to summ function and it exists as long as the function is running. However you can create another 'result' variable in main function which is local to main. Both are not in conflict. RE: I'm having trouble printing a return value in my main function - IAMK - Apr-08-2018 result is not a global variable. It is defined inside of summ(), but main() has no idea what it is. You either need to define it in main() and then pass it into summ(), or just define it globally. |