473,881 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Noob needs help on very simple question

3 New Member
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 1350
43 New Member
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"
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
  6. square(2)
Or try removing the quotes and something like this, python test.py square(2), should work
Mar 8 '08 #2
3 New Member

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
43 New Member
Django seems to accomplish this although it may do it this way.

Expand|Select|Wrap|Line Numbers
  1. import sys
  3. def sayHello(name):
  4.     print "Hello, %s!"%name
  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])
  11. main()
  13. # output in command prompt
  14. python test.py sayHello elcron
  15. Hello, elcron!
Mar 9 '08 #4
2,851 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1. # test.py
  3. def square(x):
  4.     return x*x
  6. if __name__ == '__main__':
  7.     import sys
  8.     print square(int(sys.argv[1]))
At the command prompt:

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

Mar 9 '08 #5
56 New Member
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
...instead of...
Expand|Select|Wrap|Line Numbers
  1. def squared(x):
  2. return x*x
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

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 option.
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 values (without duplicates) * count the number of records in both tables that has that value for the field.
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: ---------------------------------------------------- TypeError: addFile() takes exactly 1 argument (2 given) ----------------------------------------------------
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 base of knowledge and dont have any problem working the command line etc. I want to learn a language that I can use in my networking duties that is most likely to be of use to me. I have a few choices I can think of being:
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 want to be able to draw lines in a picturebox based upon certain data points I have received. I dragged a picturebox from the toolbar onto my form, but after having gone through the help files, looking online and trying a variety of things, I...
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 "myDB" with a table "myUsers" with fields "Username" and "Password". I also have the MySQL ODBC driver loaded with a DSN "dsnMySQL" setup. First question is can someone direct me to a site or provide a sample code for a login page that...
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 program I have a picture box that when the user clicks it I want it to change the picture, and when the user clicks it again I want it to change back to the first image. So I figured a simple if statement would work, well it doesn't. Again I'm a...
by: hstagni | last post by:
Here is a sample to explain my problem class Foo { int a; class Bar { void ChangeA() {
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.