Re: new to python and programming at large.

Discussion in 'Python' started by Dave Angel, Jan 9, 2013.

  1. Dave Angel

    Dave Angel Guest

    On 01/09/2013 05:28 PM, wrote:
    > thanks for ur help I wz able to do it.but I wish to expand it by asking a user to input a number for the sqrt to be calculated it I dd it this way but its not working.
    >
    >
    > from math import sqrt
    > number = raw_input('enter a number:')
    > def number(y):
    > return number(Y)
    >
    >
    >
    >


    Please don't keep posting new messages on the same topic. When adding
    to a thread, use Reply (and make sure python-list is in the to: or cc:
    field).

    This program has a pile of problems with it.

    You've used the name 'number' for three different purposes. Let me
    explain what those lines do.

    number = raw_input(...
    Capture a string from the user. Type is str.

    def number(y):
    toss out the string the user entered, and bind that name instead to
    a function definition that takes a parameter y.

    return number(Y)
    Attempt to call your own function recursively, but first crash since
    Y is undefined. Y is not the same as y.

    Fortunately, you never actually call the function, by any name.

    You never used the string you got from the user, but if you had fixed
    everything else, you'd have discovered that sqrt() expects either an int
    or a float.

    Suggestions:
    put your imports first
    then put your function definitions and classes
    only then put your initialization and calls to those functions and
    classes.
    Make sure you don't use the same name for two different purposes.
    It can frequently be okay, but it's certainly confusing for a beginner.
    Watch the case of any names you use. It matters.

    You don't have a spec, but perhaps this is close to what you want
    (untested):


    from math import sqrt

    def squareroot(y):
    return sqrt(y)


    number = float(raw_input('enter a number:'))
    print squareroot(number)


    --

    DaveA
     
    Dave Angel, Jan 9, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    152
  2. Ulrich Eckhardt

    new to python and programming at large

    Ulrich Eckhardt, Jan 9, 2013, in forum: Python
    Replies:
    1
    Views:
    160
  3. Chris Angelico

    Re: new to python and programming at large

    Chris Angelico, Jan 9, 2013, in forum: Python
    Replies:
    1
    Views:
    168
    Chris Angelico
    Jan 9, 2013
  4. John Gordon

    new to python and programming at large.

    John Gordon, Jan 9, 2013, in forum: Python
    Replies:
    1
    Views:
    182
  5. Replies:
    0
    Views:
    167
Loading...

Share This Page