By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

Noob needs help on very simple question

P: 3
Hi All,

Apologies in advance, this question is very rudimentary, but I can't seem to get it right.

I am running python within cygwin. I have a python script called test.py, and inside it a function defined simply as

def square(x):
return x*x
print "hello world"

My question is how do I run this script/call the function inside it?

python test.py does not do anything, I read that something like this might work:

python test.py "square(2)"

but this does not work either. Please help! Thank you in advance.
Mar 8 '08 #1
Share this Question
Share on Google+
5 Replies


P: 43
Hi All,

Apologies in advance, this question is very rudimentary, but I can't seem to get it right.

I am running python within cygwin. I have a python script called test.py, and inside it a function defined simply as

Expand|Select|Wrap|Line Numbers
  1. def square(x):
  2.     return x*x
  3.     print "hello world"
  4.  
My question is how do I run this script/call the function inside it?

python test.py does not do anything, I read that something like this might work:

python test.py "square(2)"

but this does not work either. Please help! Thank you in advance.
Functions need to be called on to run. also no code can go after the return statement.

Expand|Select|Wrap|Line Numbers
  1. def square(x):
  2.     n = x*x
  3.     print "hello world"
  4.     return n
  5.  
  6. square(2)
  7.  
Or try removing the quotes and something like this, python test.py square(2), should work
Mar 8 '08 #2

P: 3
Thanks,

So when I make the function call from within the script, it works fine. But I can't seem to use the command line to accomplish this. How can one send parameters to a python function in test.py from the command line?
I am using cygwin so I'm wondering if it's because of that.

I have tried every combination:

python test.py square(3) produces the error:

bash: syntax error near unexpected token `('

python test.py "square(3)" does nothing

python test.py square 3 does nothing

python test.py "square 3" does nothing


Any ideas would be appreciated!
Mar 9 '08 #3

P: 43
Django seems to accomplish this although it may do it this way.

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. def sayHello(name):
  4.     print "Hello, %s!"%name
  5.  
  6. def main():
  7.     if len(sys.argv) >= 2:
  8.         if sys.argv[1] == "sayHello" and len(sys.argv) == 3:
  9.             sayHello(sys.argv[2])
  10.  
  11. main()
  12.  
  13. # output in command prompt
  14. python test.py sayHello elcron
  15. Hello, elcron!
  16.  
Mar 9 '08 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. # test.py
  2.  
  3. def square(x):
  4.     return x*x
  5.  
  6. if __name__ == '__main__':
  7.     import sys
  8.     print square(int(sys.argv[1]))
At the command prompt:

>>>C:\Python23>python test.py 16
256

C:\Python23>
Mar 9 '08 #5

raubana
P: 56
Did you remember to tab the parts of the command so it knows what you're talking about?

like this:
Expand|Select|Wrap|Line Numbers
  1. def squared(x):
  2.       return x*x
  3.  
...instead of...
Expand|Select|Wrap|Line Numbers
  1. def squared(x):
  2. return x*x
  3.  
Just in case you might of missed that. That is what you ment, right?
Mar 11 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.