# nth_prime.py def nth_prime(n): primes =  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)]: primes.add(test_int) 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.