samedi 30 décembre 2017

Python script to find nth prime number

I'm new to Python and I thought I'd try to learn the ropes a bit by writing a function to find the nth prime number, however I can't get my code to work properly. No doubt this is due to me missing something fundamental, but I'd appreciate your help in finding where it went wrong!

`c=2

n=input("Which prime would you like? ")

n=int(n)

a=[]

l=len(a)

while l<=n:
    if c==2:
        a.append(c)

    elif (c % 2 ==0): #c is even
        break

    elif (c % 2 !=0): #c is odd
        if c<7:
            a.append(c)

        elif c >=7:
            for i in range(3,int((c+1)/2)):
                if (c % i ==0):
                    break
            else:
                a.append(c)
    else:            
        c+=1

a[n]`

Thanks! Andrew

Aucun commentaire:

Enregistrer un commentaire