user1125258
user1125258

Reputation:

How do I execute a function?

I'm new to the python and i was trying to do my first python function, but unfortunately i faced some problems to get the expected result from this simple function please help me to show the output of that function. the below posted function is written in the python editor

i do not know how to call this function from the python shell to show its result.

python code:

def printme( str ):
    "This prints a passed string into this function"
    print str;
    return;

python shell:

>>> printme("d")
>>> Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
printme("d")
NameError: name 'printme' is not defined

Upvotes: 0

Views: 889

Answers (5)

iratxe
iratxe

Reputation: 77

My python knowledge is very low... , you question come from this tutorial ,I have all to write as your example on a Linux shell , and i having none problem...

>>> def printme(str):
        This print .......................
        print str
        return

>>> printme('d')
d

how i have Understand , you problem is that you to prove working with idle console and a Linux shell without before your code to save....i think , the examples from shellfly and alKid describe gut , how can you solving your problem...

sorry about my English....

Upvotes: 0

aIKid
aIKid

Reputation: 28292

If you're using the unix shell:

$ cd C:\yourpath
$ python mypythonfile.py

If you are using the interactive mode, then this:

execfile("C:\\myfolder\\myscript.py")

The long way in interactive mode, but if you prefer to set your default path:

import os
prevPath = os.getcwd() #save the default path
myPath = "C:\myPython\somepath"
os.chdir(myPath) #set your python path
execfile("myscript.py") #executes the file
#os.chdir(prevPath) will restore the default path

Or did i misunderstood your question? If you just want to run a function, it's just as simple as this..

>>> def printme(str):
    print str


>>> printme("Hello world!")
Hello world!
>>> 

Hope this helps!

Upvotes: 0

Nadir Sampaoli
Nadir Sampaoli

Reputation: 5555

You have to load the script as you start the interpreter. From a terminal shell (like bash or zsh):

$ python2 -i script.py 
>>> printme("hola")
hola
>>> 



On a side note, you don't have to terminate your statements with a semicolon (if they are in their own line), neither have to append a return statement at the end of the function (since indentation and line separation are significative in Python).

Upvotes: 3

Bhavish Agarwal
Bhavish Agarwal

Reputation: 673

If you are using any of the IDEs for python, you could actually run the program in python shell by pressing/typing the Run(F5 equivalent). If that is not the case, read along:

  1. Save the program as test.py (or any other name) in any location of your choice.
  2. Start python shell
  3. >>import sys
  4. >>sys.path
  5. If the directory in which you saved the test.py is present in the output of sys.path, go to step 7
  6. sys.path.append("directory address where you saved the test.py")
  7. >>import test #note .py is removed
  8. >>test.printme("Hello World")

sys.path is the list containing all the directories where python looks for importing modules. By adding (appending) your directory you are ensuring the test.py can be imported as module test. You can then call any functions of test.py as test.fucn()

At step 7 you could have done: 7. >>from test import printme 8. >>printme("Hello again")

Upvotes: 0

shellfly
shellfly

Reputation: 962

$ cd /path/to/your/filename.py
$ python
>>> from filename import printme
>>> printme("hello world!")

Upvotes: 4

Related Questions