#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    2
    Rep Power
    0

    Angry simple square root program


    X = input("Number please? ")
    print ("the square root is"), X*X

    This is THE first program I am writing.
    I am a freshman in college and I am just interested in trying out python so if anyone could help me and/or direct me towards a good guide that is complete in laying out all the basics.

    Bryan Behrens

    The-Undead-Frog
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    1
    Rep Power
    0
    Is it something like this that your after:
    Code:
    def sqrt(y):
        return y**0.5
        
    X = input("Number please? ")
    print ("the square root is"), sqrt(X)
    raw_input()
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by the-undead-frog
    X = input("Number please? ")
    print ("the square root is"), X*X

    This is THE first program I am writing.
    I am a freshman in college and I am just interested in trying out python so if anyone could help me and/or direct me towards a good guide that is complete in laying out all the basics.

    Bryan Behrens

    The-Undead-Frog
    Did you try running your program? It works just as you would expect.

    The brackets round the string in the print statement is unneccessary, but are ignored. It is also conventional to start variable names with a lower case letter, so use x rather than X.

    There are lots of resources for people new to Python. The tutorial that comes as part of the docs is very good, and there are lots of other resources on the web (but I do not have any URLs to hand).

    Dave - The Developers' Coach
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Reutlingen, Germany
    Posts
    10
    Rep Power
    0
    Originally Posted by Karl_bertil
    Is it something like this that your after:
    Code:
    def sqrt(y):
        return y**0.5
        
    X = input("Number please? ")
    print ("the square root is"), sqrt(X)
    raw_input()
    or

    Code:
    x = input("Number please? ")
    print "The square root is ", (lambda y: y ** 0.5)(x)
    raw_input()
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    2
    Rep Power
    0
    yes I did run it and it would just ask me "number please?"
    then it wouldnt return any info when I hit enter after I put a number in

    x = input("Number please? ")
    print "The square root is ", (lambda y: y ** 0.5)(x)
    raw_input()

    can you explain what I did wrong and why that is right?

    whats lambda = ?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Reutlingen, Germany
    Posts
    10
    Rep Power
    0
    Originally Posted by the-undead-frog
    yes I did run it and it would just ask me "number please?"
    then it wouldnt return any info when I hit enter after I put a number in

    x = input("Number please? ")
    print "The square root is ", (lambda y: y ** 0.5)(x)
    raw_input()

    can you explain what I did wrong and why that is right?

    whats lambda = ?
    plz copy & paste the original code I supplied and save it as e.g. 'sqrt.py' and run it via 'python sqrt.py' ... you probably have a typo somewhere b/c I tested the exact same code here and it works just fine....

    lambda is a one-line-mini-function(TM) ... I will try to explain this with a little code evolution

    Code:
    def sqrt(y):
            return y ** 0.5 
    
    .... # and later when we need it we call it like
    sqrt(x)
    ... so for that one-line function we have that whole work...

    Code:
    g = lamda y : y ** 0.5
    .... # and later when we need it we call it like
    g(x)
    ...looks a bit better ... we just used 2 lines instead of 3

    Code:
    (lambda y: y ** 0.5)(x)
    ....hush.. now we are at the real deal... one line instead of three with the least use of additional variables and whatnot...

    ok?

    ... check out Python.org and go trough all the documentation you can find ...

    I'd also suggest you buy 'Dive into Python' by Mark Pilgrim if you're really interested in it... greetz
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    It may be worth mentioning that where lambda can be useful in some situations, if your going for preformance then your not gonna want to use it too often. So, if you have a function you want to call more than once. It's best to use the standard function declaration.

    Note: This observation is based on documents describing earlier versions of Python. But i wouldn't expect this to change too much.

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo