-3

I want to store all pairs of integers whose sum is equal to N as tuples. Here is my code so far:

m = []
l = []
degree = 9
for i in range(0, degree):
    m += [degree - i];
    l += [i]
    pairs = (m[i]),(l[i])    
pairs

This code return only last pair:

(1, 8)

What I want is this:

(9, 0),(8, 1),(7, 2),(6, 3),(5, 4),(4, 5),(3, 6),(2, 7),(1, 8)

Can you help me identify and fix the error in my code?

Swifty
  • 2,630
  • 2
  • 3
  • 21
feration48
  • 145
  • 7
  • 1
    Your last statement, `pairs` only returns the _latest_ value of pairs. Instead of that, you could for example do `print([(m1,l1) for m1,l1 in zip(m,l)])` – Swifty Dec 11 '22 at 15:25
  • 1
    You are rewriting `pairs` with each iteration. You need to initialize it ***before*** the loop, and `append` to it – Tomerikoo Dec 11 '22 at 15:26
  • The code shown here makes no sense at all, and has multiple issues. If you want to fix the code, that does not make an appropriate question here; we need questions about **one** issue which you have *already identified* - we don't do tutoring. If you are instead just asking about how to get the result, it would be better not to show code like this - but the question is still too broad: solving the problem involves following multiple logical steps. – Karl Knechtel Dec 11 '22 at 15:28
  • @KarlKnechtel: allow me to disagree; this code, though not optimal, almost does the intended job; the main (and let's say only) issue is the final statement, that doesn't yield the intended result. – Swifty Dec 11 '22 at 15:31
  • Oh, I see the approach now. That's... strange, but it would otherwise work, yes. In that case, the question is still surely a duplicate; the basic technique here is covered by e.g. [How can I use `return` to get back multiple values from a loop? Can I put them in a list?](/questions/44564414). This code is not in a function, so the setup is a bit different, but the conceptual problem and the solution are the same. – Karl Knechtel Dec 11 '22 at 15:37
  • That said, feration48, shouldn't your code also return (0,9) ? @KarlKnechtel: indeed the code is 'strange' (which I would attribute to beginner's clumsiness; I've often written clumsy code by overthinking things, and still do from time to time :) ). – Swifty Dec 11 '22 at 15:37
  • @Swifty Yes, it should return (0,9) too. I don't understand why that's not the last output... – feration48 Dec 11 '22 at 15:46
  • "I don't understand why that's is not the last output." Please start by **reading the documentation** for `range`, in order to understand what numbers are `in range(0, degree)`, and thus what values `i` will have each time through the loop. – Karl Knechtel Dec 11 '22 at 15:48

1 Answers1

0

You can do it this way by defining a new pair's list, appending it inside the loop, and finally printing.

m = []
l = []
degree = 9
pairs = []
for i in range(0, degree):
    m += [degree - i];
    l += [i]
    pairs.append((m[i],l[i]))
print(pairs)
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103