Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36513 > unrolled thread

Re: new to python and programming at large.

Started byDave Angel <d@davea.name>
First post2013-01-09 13:00 -0500
Last post2013-01-09 13:00 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: new to python and programming at large. Dave Angel <d@davea.name> - 2013-01-09 13:00 -0500

#36513 — Re: new to python and programming at large.

FromDave Angel <d@davea.name>
Date2013-01-09 13:00 -0500
SubjectRe: new to python and programming at large.
Message-ID<mailman.332.1357754467.2939.python-list@python.org>
On 01/09/2013 05:28 PM, kwakukwatiah@gmail.com 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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web