1

Currently, I have this code:

for i in range(lower_limit, upper_limit+1): 

    for j in range(0,len(prime_number)):
        for k in range(0 + j,len(prime_number)):
            if i == prime_number[j] + prime_number[k] and i % 2 == 0:  

                print(i, "=", prime_number[j], "+", prime_number[k])

which prints:

10 = 3 + 7 
10 = 5 + 5 
12 = 5 + 7 
14 = 3 + 11 
14 = 7 + 7 

I need the result to look like:

10 = 3 + 7 = 5 + 5
12 = 5 + 7
14 = 3 + 11 = 7 + 7

I know I have to use end = " " somehow, but then it prints all the numbers in just one line. What do I do?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Joon Choi
  • 191
  • 1
  • 2
  • 6
  • 1
    Possible duplicate of [How can I suppress the newline after a print statement?](http://stackoverflow.com/questions/12102749/how-can-i-suppress-the-newline-after-a-print-statement) – Harsha W Mar 24 '17 at 06:50
  • You don't **have** to use end. I would suggest building a `list` along the lines of `[(prime1, [tuple1, tuple2, ...]), (prime2, [tuple1]), ...]` and then using that to build entire strings in one go. – TemporalWolf Mar 24 '17 at 07:06

1 Answers1

4

Use end= " ", and an empty print() at the end of the outer loop to end the line.

For example:

>>> for i in range(3):
...     print("foo", end = " ")
...     print("bar", end = " ")
...     print()
...
foo bar
foo bar
foo bar

Specifically for your case:

for i in range(lower_limit, upper_limit+1): 
    print(i, end = " ") 
    for j in range(0,len(prime_number)):
        for k in range(0 + j,len(prime_number)):
            if i == prime_number[j] + prime_number[k] and i % 2 == 0:  

                print("=", prime_number[j], "+", prime_number[k],end = " ")
    print()
Junuxx
  • 14,011
  • 5
  • 41
  • 71