Try... except....Try again?

Xavier Ho contact at xavierho.com
Fri Jul 17 10:35:24 EDT 2009


oops, wrong address.

When will reply-to tag appear on the Python mailing list? =/

Good idea, Steven and MRAB.

I changed my code to the following:

    def nPrime(self, n):
        "Returns nth prime number, the first one being 2, where n = 0. When
n = 1, it returns 3."
        for x in range(n+2):
            try:
                return self.primes[n]
            except IndexError:
                self.next()

And it's definitely better. Thanks a ton guys.

(n+2 because after looping the maximum number of times, it needs to try
returning the value again.)

It works just as well as the previous version, but slightly safer on my
part... and I can adapt this in my future coding habits.

Thanks again.


Ching-Yun "Xavier" Ho, Technical Artist

Contact Information
Mobile: (+61) 04 3335 4748
Skype ID: SpaXe85
Email: contact at xavierho.com
Website: http://xavierho.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090717/b5fa2eaf/attachment.html>


More information about the Python-list mailing list