TechYoYo

Tech for the Cool

My first real Python program

Posted on | August 26, 2009 | 8 Comments

I am really beginning to like Python Programming Language. It is quite efficient in doing lot of tasks. Unfortunately, I don’t get to devote much time for learning it. Here is the code that will calculate the square root by newton method of iterations.

import random

a = float(raw_input(“enter a integer”))

x = random.randint(1,a-1)

while True:

print x

y = (x + a/x) / 2

if y == x:

break

x = y

raw_input()

The ‘raw_input()’ on last line has been used as an equivalent to ‘getch()’ of C.  The only problem with this code is that it will not accept floating numbers. What should I to make it compute even floating numbers? Please write in the comment section.
Here is my post about Why I decided to learn Python.
If you know about any good e-book about python please tell me in the comments section.
SocialTwist Tell-a-Friend

Comments

8 Responses to “My first real Python program”

  1. Francois Trahan
    August 26th, 2009 @ 11:06 am

    You should pass only integer arguments to randint and also, you should not use ‘y == x’ as a stop condition since you’re using floating point arithmetics… try something like abs((y-x)/x) < T where T is your "tolerance", let's say 1% or 0.00001%… Your teacher will prefer this ;)

  2. David Goodger
    August 26th, 2009 @ 12:49 pm

    When you run the program and enter a floating point number, you should see a traceback, like this one:

    Traceback (most recent call last):
    File “nr.py”, line 4, in
    x = random.randint(1,a-1)
    File “/usr/lib/python2.5/random.py”, line 215, in randint
    return self.randrange(a, b+1)
    File “/usr/lib/python2.5/random.py”, line 171, in randrange
    raise ValueError, “non-integer stop for randrange()”
    ValueError: non-integer stop for randrange()

    The last line tells you what the error was, and the indented text above it tells you where it happened. The first section (starting with “File”) tells you where in the topmost file the error happened: your program called random.randint. The second section tells you that random.randint called the randrange function. And the third/last section tells you that randrange has a problem with non-integers and raised an exception.

    Look up the documentation for random.randrange and you’ll see that it requires integer bounds, but you’re supplying a floating-point (non-integer) upper bound. To fix this, try replacing:

    x = random.randint(1,a-1)

    with:

    x = random.randint(1, int(a-1))

    The code still won’t work with certain input values: try 0, 1, -5, etc. See if you can get your code to work properly (at least, not crash) with such values. Good luck!

  3. manatlan
    August 26th, 2009 @ 12:53 pm
  4. photosinensis
    August 26th, 2009 @ 12:55 pm

    If you know a bit about other programming languages (and considering that you’re a CS undergrad, you should), I recommend Mark Pilgrim’s Dive Into Python, which is the authoritative book on the language. It’s totally free (Mr. Pilgrim himself is a major player in the free and open source software movements), and if you’re on an Ubuntu box, you’ll find it at /usr/share/doc/diveintopython/html/, with the example code at /usr/share/doc/diveintopython/examples (because Python is the backbone of Ubuntu’s not-quite-SDK). That book covers most of Python’s standard library, including (I believe) Tkinter and sqlite3.

    Good luck with your Python endeavors.

  5. David Ziegler
    August 26th, 2009 @ 12:56 pm

    randint won’t take floats. Try:
    x = random.randint(1,int(a-1))

    Also, I’m not sure if you want to use y==x as your stopping condition. Probably better to use

    if abs(y-x) < e:
    break

    where e is some small number. Good luck!

  6. Tom Ritchford
    August 26th, 2009 @ 2:45 pm

    Hmm, didn’t look to hard at the documentation, did we?

    raw_input() returns a string – not a float.

    Try:

    1. float(raw_input())
    2. eval(raw_input())
    3. input() (same as 2)

    1. is probably the best choice as it’s clearest and you’ll end up with either a float or an error.

  7. arun kamath
    August 27th, 2009 @ 5:36 am
  8. Encrypt message using Python | TechYoYo
    August 28th, 2009 @ 7:17 am

    [...] can check out the code for finding out root of a number using Newton’s method of iteration in Python. This code cipher code is from the free ebook Invent Your Own Computer Games with [...]

Leave a Reply