Noob needs help on very simple question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jackinoob
    New Member
    • Mar 2008
    • 3

    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.
  • elcron
    New Member
    • Sep 2007
    • 43

    #2
    Originally posted by jackinoob
    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

    [code=python]
    def square(x):
    return x*x
    print "hello world"
    [/code]
    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.

    [code=python]
    def square(x):
    n = x*x
    print "hello world"
    return n

    square(2)
    [/code]
    Or try removing the quotes and something like this, python test.py square(2), should work

    Comment

    • jackinoob
      New Member
      • Mar 2008
      • 3

      #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!

      Comment

      • elcron
        New Member
        • Sep 2007
        • 43

        #4
        Django seems to accomplish this although it may do it this way.

        [code=python]
        import sys

        def sayHello(name):
        print "Hello, %s!"%name

        def main():
        if len(sys.argv) >= 2:
        if sys.argv[1] == "sayHello" and len(sys.argv) == 3:
        sayHello(sys.ar gv[2])

        main()

        # output in command prompt
        python test.py sayHello elcron
        Hello, elcron!
        [/code]

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          [code=Python]# test.py

          def square(x):
          return x*x

          if __name__ == '__main__':
          import sys
          print square(int(sys. argv[1]))[/code]
          At the command prompt:

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

          C:\Python23>

          Comment

          • raubana
            New Member
            • Feb 2008
            • 55

            #6
            Did you remember to tab the parts of the command so it knows what you're talking about?

            like this:
            [CODE=python]
            def squared(x):
            return x*x
            [/CODE]
            ...instead of...
            [CODE=python]
            def squared(x):
            return x*x
            [/CODE]

            Just in case you might of missed that. That is what you ment, right?

            Comment

            Working...