Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: new to python and programming at large. (http://www.velocityreviews.com/forums/t956361-re-new-to-python-and-programming-at-large.html)

Dave Angel 01-09-2013 06:00 PM

Re: new to python and programming at large.
 
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



All times are GMT. The time now is 07:42 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.