``````# nth_prime.py

def nth_prime(n):
primes = [2]

test_int = 1
while len(primes) < n:
test_int += 2
for p in primes:
if test_int % p is 0:
break
else:
if p is primes[len(primes)]:

return primes[n - 1]

while True:
try:
num = int(input("Enter a number:\n"))
print(nth_prime(num))
except ValueError:
print("NaN.  Try again!")``````

It should add prime numbers to the list until the length of the list is equal to n and return the last number in the list. For some reason it crashes once a number is entered. I've looked over it but I can't seem to find what's wrong.

``````if 2 == 3:
return True
else
return False``````
``len(primes) = 1 but list index start at 0 --> primes[len(primes) - 1]:``
``list.append(element)``

It works now, but for some reason crashes with large numbers (the 10001st prime).

Also you are using "is" instead of equal. "is" tests for the same object, so can not be relied upon for an equal condition. Finally, the statement

``````else:
if p is primes[len(primes)]:``````

is unnecessary. The for loop takes care of that. So, you could use an indicator like this instead.

``````prime_found = True
for p in primes:
if test_int % p is 0:
prime_found = False
break
if prime_found:
primes.append(test_int)
##
##-------------------------------------------------------------------
##   a short example of "is" vs. "=="
x = "abc"
y = "abcd"
print x is y, id(x), id(y)        ## prints False
print x is y[:3], id(x), id(y)    ## prints False
print x == y[:3], x, y[:3]        ## prints True``````

but for some reason crashes with large numbers (the 10001st prime)

That is possibly the result of using "is". Python stores smaller numbers and points to them, so "is" works on smaller numbers since they all point to the same object, but once you get above the stored value it no longer works. This code works for me:

``````def nth_prime(n):
primes = [2]

test_int = 1
while len(primes) < n:
test_int += 2
prime_found = True
for p in primes:
if test_int % p == 0:
prime_found = False
break
if prime_found:
primes.append(test_int)

return primes[n - 1]

print( nth_prime(10002) )

"""
prints 104759 which is on the list here
http://www.cs.arizona.edu/icon/oddsends/primes.htm
I'm not going to count them to see if it really is number 10,002
"""``````