All pairs (as tuples) of integers adding to a given integer N
Question:
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?
Answers:
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)
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?
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)