2
def is_prime(x):     
    count = 1
    my_list = []
    while count > 0 and count < x:
        if x % count == 0:
            my_list.append(x/count)
        count += 1
    return my_list
my_list = is_prime(18)

def prime(x):

    my_list2 = []
    for number in my_list:
        if number <= 2: 
            my_list2.append(number)
        else:
            count = 2
            while count < number:
                if number % count == 0:
                    break
                else: 
                    my_list2.append(number)
                count += 1
    return my_list2
print prime(18)

Just started out with Python. I have a very simple question.

This prints: [9, 3, 2].

Can someone please tell me why the loop inside my else stops at count = 2? In other words, the loop inside my loop doesn't seem to loop. If I can get my loop to work, hopefully this should print [2, 3]. Any insight is appreciated!

2
  • What are is_prime and prime and supposed to do? Commented Mar 19, 2014 at 9:28
  • sorry I forgot to explain. So this is a program hat is supposed to get the prime factors of a number. is_prime gets all the factors while prime supposedly gets all the prime factors. Commented Mar 19, 2014 at 9:39

1 Answer 1

3

Assuming that my_list2 (not a very nice name for a list) is supposed to contain only the primes from my_list, you need to change your logic a little bit. At the moment, 9 is being added to the list because 9 % 2 != 0. Then 9 % 3 is tested and the loop breaks but 9 has already been added to the list.

You need to ensure that each number has no factors before adding it to the list.

There are much neater ways to do this but they involve things that you may potentially find confusing if you're new to python. This way is pretty close to your original attempt. Note that I've changed your variable names! I have also made use of the x that you are passing to get_prime_factors (in your question you were passing it to the function but not using it). Instead of using the global my_list I have called the function get_factors from within get_prime_factors. Alternatively you could pass in a list - I have shown the changes this would require in comments.

def get_factors(x):
    count = 1
    my_list = []
    while count > 0 and count < x:
        if x % count == 0:
            my_list.append(x/count)
        count += 1
    return my_list

# Passing in the number                   # Passing in a list instead
def get_prime_factors(x):                 # get_prime_factors(factors):
    prime_factors = []
    for number in get_factors(x):         #     for number in factors:
        if number <= 2:
            prime_factors.append(number)
        else:
            count = 2
            prime = True
            while count < number:
                if number % count == 0:
                    prime = False
                count += 1
            if prime:
                prime_factors.append(number)

    return prime_factors

print get_prime_factors(18)

output:

[3, 2]

Just to give you a taste of some of the more advanced ways you could go about doing this, get_prime_factors could be reduced to something like this:

def get_prime_factors(x):
    prime_factors = []
    for n in get_factors(x):
        if n <= 2 or all(n % count != 0 for count in xrange(2, n)):
            prime_factors.append(n)
    return prime_factors

all is a built-in function which would be very useful here. It returns true if everything it iterates through is true. xrange (range on python 3) allows you to iterate through a list of values without manually specifying a counter. You could go further than this too:

def get_prime_factors(x):
    return [n for n in get_factors(x) if n <= 2 or all(n % c != 0 for c in xrange(2, n))]
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! this makes a lot more sense now.
@user3436782 happy to help. I've added some alternatives too.
Thanks for the explanation. I've seen some of these commands before (not sure if commands is the word used for programming) but the tricky part is applying them in this situation.
Tom, I noticed how in get_prime_factors(x) you call the get_factors(x) function, in other words, a function within a function. Is this possible because both have the same input in (x)? I'm guessing if the inputs were different, I'd need to call each function separately?
@user3436782, yes you would. I've added some more detail to my answer explaining this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.