954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

complete novice help required please

Hi All
I am trying to learn Python and cannot get aroud this problem for the life of me.
I have generated a sript as follows, called xandy.py
def xandy (x,y):
if x < y:
print x, "is less than", y
elif x > y:
print x, "is greater than", y
else:
print x, "and", y, "are equal"
I cannot get it to work in the shell?
>>> x=5
>>> y=4
>>> xandy
Traceback (most recent call last):
File "", line 1, in
NameError: name 'xandy' is not defined
>>> xandy (5,4)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'xandy' is not defined
>>>
Thanks in anticipation

chico2009
Newbie Poster
18 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

2 ways at least: you can type

>>> from xandy import xandy

and then use your function. The file must be in a place where python will find it (in a folder listed in your PYTHONPATH environment variable.
Second (and best) way, run python's built-in gui IDLE (look in the subfolder idlelib of your python library). When idle has started, choose to open your program in the file menu, then select RUN MODULE in the Run menu and your program will run in the shell.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

First of all chico2009, please enclose your code in proper tags, for more on that refer to this link .

Now, moving onto your question, the only thing you have to change is:

x=5
y=4
xandy(x,y)
sravan953
Posting Whiz in Training
243 posts since May 2009
Reputation Points: 2
Solved Threads: 30
 

Hi both
Thanks very much for your help, geting there slowly

chico2009
Newbie Poster
18 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: