-2

Here is my version of the code right now and I keep getting list index error.

n = 0
y = len(list1)-1
while n < y:
    for k in list1:
        if list1[n]+ k == g:
            print("The 2 prime numbers that add up to ",g,"are ", list1[n]," and ",k,".")
            break
        else:
            n = n+1
2
  • 2
    What kind of list index error? Where? What is the exact error message? Commented Feb 25, 2014 at 1:33
  • if list1[n]+ k == g : IndexError: list index out of range Commented Feb 25, 2014 at 1:39

4 Answers 4

2

You are incrementing n in the for loop but testing its contraint in the outer while loop.

Perhaps this is what you wanted:

n = 0
y = len(list1)-1
found = 0
while n < y:
    for k in list1:
        if list1[n]+ k == g:
            print("The 2 prime numbers that add up to ",g,"are ", list1[n]," and ",k,".")
            found = 1
            break # for loop
    if found:
       break # while loop
    n = n + 1

A much better way to do it is using itertools.combinations_with_replacement:

import itertools
for (v1,v2) in itertools.combinations_with_replacement(list1, 2):
    if v1 + v2 == g:
        print("blah blah blah")
        break

combinations_with_replacement(list1,2) will return all the unordered combinations of two elements of list1. For instance, combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC

Sign up to request clarification or add additional context in comments.

2 Comments

in main if list1[n]+ k == g : IndexError: list index out of range
i see now, thank u soo much! i think the loop now works
0

The problem is that you're only breaking out of the inner loop when you find a pair of elements that add up to g. The outer loop repeats, and then the inner loop will increment n past the length of the list.

You can set a variable that stops the outer loop as well. You also shouldn't be incrementing n in the inner loop at all, only in the outer loop, so you can just use a for loop.

y = len(list1)-1
found = False

for n in range(y):
    if found:
        break
    for k in list1:
        if list1[n]+ k == g:
            print("The 2 prime numbers that add up to ",g,"are ", list1[n]," and ",k,".")
            found = True
            break
    n = n+1

Comments

0

Python for-loops can have an else-statement, so just move your else to be at the same indent as the for-loop:

n = 0
y = len(list1)-1
while n < y:
    for k in list1:
        if list1[n]+ k == g:
            print("The 2 prime numbers that add up to ",g,"are ", list1[n]," and ",k,".")
            break
    else:
        n = n+1
        continue
    break

Comments

-1

You left a few bits of information out, but I gather that you are trying to find 2 primes that match a target. In order to access a list in this manner, you need to enumerate it.

y = len(list1) - 1
while n < y:
    for n, k in enumerate(list1):
        if list1[n]+ k == g :
            print("The 2 prime numbers that add up to ",g,"are ", list1[n]," and ",k,".")
            break

However, you don't really need the index, two for loops would accomplish the same thing.

target = 8
primes = [2, 3, 5, 7, 11, 13, 17, 19]
message = 'The 2 prime numbers that add up to {target} are {value1} and {value2}'
for index1, value1 in enumerate(primes):
    for value2 in primes[index1 + 1:]:
        if value1 + value2 == target:
            print(message.format(target=target, value1=value1, value2=value2))

2 Comments

The while condition doesn't really make sense in your first code snippet. It will always be true the first time through the loop (unless list1 has less than 2 elements), and false the second time.
And list1[n] is always the same as k. That code is just wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.