[Tutor] help beginner in python.

Steve Willoughby steve at alchemy.com
Sun Feb 24 22:57:18 CET 2008


Krystle Scott wrote:
> i need to write an algorithm for computing square roots.
> so far I have

This sounds like a class exercise.  I think we can help you with
Python questions, but you'll need to do the part that is directly
related to your homework on your own.

> import math
> 
> def main ():

Python doesn't explicitly need a main() function.  You can just
define what other classes and functions you need, and call them
from your mainline (outer-level) script code.

>       print " This program computes approximate square roots using Newton's
> method: "
>       print
> 
> 
>      input = (" Enter whose square root you wish to compute (a) : ")
>      input = (" Enter the number of iterations :" )

I suggest going through some of the Python tutorial material available
on python.org.  Going first through the getting-started material would 
be a lot of help for you here.

These are just assigning strings to a variable called "input" which
isn't really accomplishing anything for you.

If you wanted to actually prompt for input and receive it, you need
to call the input() function, like this:

x = input("Enter a number: ")

>      discRoot = math.sqrt

math.sqrt, like any function, needs to be passed a number to take
the square root of, like math.sqrt(x).  However, I suspect that's
only going to help you check your work.  You said you need to code
an algorithm for computing square roots, so just calling the
sqrt() function from the library isn't accomplishing that goal,
and won't get you much credit for your class.  You will need to
figure out how that algorithm works, and define your own function
to implement it.  Call math.sqrt() to check your result, if you
want.


> 
> print
> print "The solutions of the square roots"
> 
> 
> I need to have it print out iterations and math.sqrt what am i doing wrong?

To actually do iterations you'll want a for loop.  To print values, you
need to actually mention them in the print statements, like:

print "The value of x is", x




More information about the Tutor mailing list