I have a simple class that generates prime numbers, using memorisation and iteration to generate the next prime number.<br><br>In the class, I have defined a function that gives, say, the 5th prime number. Or the 1000th, it's still very fast. But the code isn't what I really like.<br>
<br>    def nPrime(self, n):<br>        "Returns nth prime number, the first one being 2, where n = 0. When n = 1, it returns 3."<br>        while True:<br>            try:<br>                return self.primes[n]<br>
            except:<br>                self.next()<br><br>The next() function generates the next prime, and appends it into primes. This way, it keeps trying to append until the nth prime requested exist, and returns it.<br>
<br>My problem is that it's a "While True" loop, which I get a lot of "Don't do it!" In the light of making the code better, what could I do?<br><br>Best regards,<br><br clear="all">Ching-Yun "Xavier" Ho, Technical Artist<br>
<br>Contact Information<br>Mobile: (+61) 04 3335 4748<br>Skype ID: SpaXe85<br>Email: <a href="mailto:contact@xavierho.com">contact@xavierho.com</a><br>Website: <a href="http://xavierho.com/">http://xavierho.com/</a><br>