In Basic there is a command gosub (go to a subroutine). I can not find the proper command in Python to call another file, run it then come back to the next line in the main program where it went to the subroutine. What is the proper instruction?
Be sure to click 'more' and select 'suggest as answer'!
If you're the thread creator, be sure to click 'more' then 'Verify as Answer'!
In Basic there is a command gosub (go to a subroutine). I can not find the proper command in Python to call another file, run it then come back to the next line in the main program where it went to the subroutine. What is the proper instruction?
I'm not a Python programmer, but I think you want to define a function using "def".
thanks John I will read more on def.
do I just code in def (filename.py) and what gets me back to the main program?
Anyone else care to give me advice. Can "call" be used and how?
Here's the chapter on defining and calling functions at Wikibooks: http://en.wikibooks.org/wiki/Python_Programming/Functions
This is for calling functions within the same file. I don't know how you'd call functions defined in another file.
Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.
for example: fibo.py has below lines of code
deffib(n):# write Fibonacci series up to n
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a+b
print()
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
returnresult
and you can create your main file in which you need to import this fibo.py
import fibo
and access the function by calling it as below:
fibo.fib(1000)
for more help you can refer to the below link chapter-6:
thanks bheemarao
I couldn't make these instructions work:
Here is what I am trying to do:
I have a stand alone program that will run using the command (on LX Terminal) sudo ./filename.py but if I try to write a main program to call this program nothing I have tried seems to work. I have read the Python tutorial section on Modules many times but can not come up with the correct way to call this standalone program then continue with the main program. I thank all of you for your input but am still at a loss.