[Tutor] Help With A Program

Caleb Benz cjbenz310 at gmail.com
Fri Apr 2 23:11:55 EDT 2021


Hello there! 

I am currently writing a program that takes an integer, n, from a user, and outputs all of the prime numbers up to and including n. I have written the following code, and am not sure where I am going wrong. For some reason, my program isn’t responding to the fact that x is going up 1 each time, and therefore isn’t working. Here is the code:

def generatePrimes():
    n=int(input('Enter a number:'))
    integer_list=list(range(2,(n+1)))
    result=[2,3]
    x=1
    for i in range(2,(n+1)):
        x=x+1
        if i==(((6*x)-1) or ((6*x)+1)):
            result.append(i)
    print(result)
generatePrimes()

Please let me know where I’m going wrong, and how I can fix this!

Thanks,
Caleb


More information about the Tutor mailing list