Python Python code for generating prime# list and evaluting a number

AI Thread Summary
The discussion revolves around a Python code snippet designed to generate a list of prime numbers and allow users to check if a specific number is in that list. Initially, the user was prompted to enter a number to check for its presence in the prime list while the list was still being generated, which caused confusion. Suggestions were made to restructure the code by moving the user input for checking numbers outside the prime list generation loop. The revised code effectively separates the prime number generation from the user input, allowing the user to first create the list and then check for numbers. The final implementation includes a loop that repeatedly prompts the user for a number until they choose to exit, ensuring a smoother user experience. Overall, the code functions correctly, but there are ongoing discussions about optimizing it further.
mr.me
Messages
49
Reaction score
0

Homework Statement


I am attempting to allow a user to enter a number and check to see if it is in a list of already generated prime numbers Here's my attempt

Where can I put the code to allow the number to be checked, the prime list generator works fine alone
Code:
def buildPrimeList ():
    
    primeList = [1, 2]
    possiblePrime = 3
    print "To calculate a list of prime values less than or equal a number..."

    x=int(raw_input("Enter a number higher then 3   "))
   
    while (possiblePrime <= x):  # changed input method
        divisor = 2
        isPrime = True
        y= int(raw_input("To see if a number is in the list enter that number enter -1 to exit: "))       
        while (divisor < possiblePrime ): #change condition to only less than possible prime
            if ((possiblePrime % divisor) == 0): 
                isPrime = False
          divisor = divisor + 1
        
        if (isPrime):
            primeList.append(possiblePrime)
            print primeList
            
        if y== -1:
            print "Goodbye"
            break
        if y in primeList:
            print y, "Is in the list"
        if y not in primeList:
            print y, "Is not in the list"    
                 
      possiblePrime = possiblePrime + 2
        
            
    return primeList
   
    
buildPrimeList()
 
Last edited:
Technology news on Phys.org
Why are you asking them to enter a number to check if it's in the list before you are done building the list? Why don't you move the y stuff outside of the loop that builds the list?
 
The user is supposed to be repeatedly prompted, that is why the y=raw_input is in the loop
 
It's still in the wrong place. Make a loop outside of buildPrimeList and put your input there.
 
Thanks,

I did this and it works well enough, any general suggestions on the code

Code:
def buildPrimeList ():
    
    primeList = [1, 2]
    possiblePrime = 3
    print "To calculate a list of prime values less than or equal a number..."
    
    x=int(raw_input("Enter a number higher then 3   "))
    
    while  (possiblePrime <= x):  # for personal preference changed range to raw_input, so that the range can be arbitrary, though at a certain point a large range will consume a-lot of memory
        divisor = 2
        isPrime = True
              
        while (divisor < possiblePrime ):
            if ((possiblePrime % divisor) == 0): 
                isPrime = False
            divisor = divisor + 1
        if (isPrime):
            primeList.append(possiblePrime)
        
        possiblePrime = possiblePrime + 2
    while (primeList==primeList):   
        y= int(raw_input("To see if a number is in the list enter that number enter -1 to exit: "))    
        if y== -1:
            print "Goodbye"
            break
        if y in primeList:
            print y, "Is in the list"
        if y not in primeList:
            print y, "Is not in the list"
           
    return primeList
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top