[Tutor] execute a function
Steven D'Aprano
steve at pearwood.info
Mon Sep 26 13:15:07 CEST 2011
Sajjad wrote:
> Hello forum,
>
>
> It has been two days that i have started with python and i am stuck with the
> following simple issue:
>
> i have created a python file and inside the file i have defined a function
> as follows:
>
> ////////////////////////////////////////////////////////////
> def greeting():
> print "Welcome to python"
>
> ///////////////////////////////////////////////////////////
>
> I am following the Book named - "Rapid GUI programming with Python and Qt"
>
> In the book they mentioned that in the command line if i type
>
> greeting() i should get the output "Welcome to python"
>
> But i get nothing.
"Nothing"? Are you sure?
You shouldn't get nothing. You should get either "Welcome to python", or
an error message. But you shouldn't get nothing at all.
First off, you need to launch the Python interactive interpreter from
the command line. I don't know what operating system you are using, but
this is how I do it from Linux:
Start menu > System > Terminal
(terminal window opens)
At the $ prompt, type: "python" and then press the Enter key. The
terminal shows:
[steve at sylar ~]$ python
Python 2.5 (r25:51908, Nov 6 2007, 16:54:01)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Now, I don't know what you have called your Python file, so I will make
up a name: "myfile.py". Make sure it is saved in your home directory.
At the >>> prompt, type "import myfile" (WITHOUT the .py) and then hit
Enter. The terminal shows:
>>> import myfile
>>>
At the >>> prompt, type "myfile.greeting() and hit Enter:
>>> myfile.greeting()
Welcome to python
>>>
Now type "from myfile import greeting", Enter, then "greeting()", Enter:
>>> from myfile import greeting
>>> greeting()
Welcome to python
>>>
Remember to change the word "myfile" to whatever the actual name of your
file is. The file MUST have a .py extension, but inside Python you never
use the .py.
If you get any errors, please COPY AND PASTE them and send them to the
mailing list. Don't type them by memory, paraphrase them, or otherwise
edit them.
Good luck!
--
Steven
More information about the Tutor
mailing list