473,378 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Noob needs help on very simple question

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
5 1331
elcron
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
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
elcron
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
2,851 Expert Mod 2GB
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
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

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

Similar topics

8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
4
by: Justin Koivisto | last post by:
OK, out of my element here once again... I want a query (or something I can use as a record source for a report) to do the following: * look in 2 tables (one field each) to find all entered...
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
10
by: stylecomputers | last post by:
Hey guys, I am absolutely new to Linux programming, with no w######s programming experience except a small amount of C++ console apps. Reasonably new to Linux, BSD etc, got good sound networking...
9
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
9
by: Ben | last post by:
Hello, I'll bet this has been asked a million times but I can't seem to find a thread that gives the clear example I need. This PC has MySQL and IIS configured and running. The MySQL database is...
0
by: irtehnoob | last post by:
Hi there, I'm pretty new to c# (started up a few days ago) and I'm making a simple program, so far everything is going good except I can't figure somethine out.. hence why I'm here. Anyways, in my...
2
by: hstagni | last post by:
Here is a sample to explain my problem class Foo { int a; class Bar { void ChangeA() {
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.