-5
1 ) input: 3 , return [1, 2, 3]
then continue 
2) input: 2, return [2, 4, 3]
then continue 
3) input: 6, return [3, 6, 6, 4, 5, 6]
then continue 
4) input: 1, return [4, 6, 6, 4, 5, 6]
then continue 
5) input: 1, return [5, 6, 6, 4, 5, 6]

My code :

1)

list_num = [ int(i) for i in input('enter numbers divided by space ').split()]

print(list_num)
lst = []

# number of elemetns as input
n = int(input("Enter number of elements : "))
 
# iterating till the range
for i in range(0, n):
    ele = int(input())
 
    lst.append(ele) # adding the element
     
print(lst)

test_list1 = []
n2 = int(input("Enter number of elements2 : "))

for i2 in range(0, n2):
    ele2 = int(input())
 
    test_list1.append(ele2) # adding the element

print(test_list1)

res_list = [lst[i] + test_list1[i2] for i in range(len(lst))]

print(res_list)

but not dynamic

Pygirl
  • 12,969
  • 5
  • 30
  • 43
toto p
  • 1
  • 2

1 Answers1

0

Your code 2 seems to be doing a pair-wise list addition.
But the computation of your res_list is using the i2 index, it should be just i.

Without the fix : [4, 5] plus [0, 1] gives [5, 6].
With the fix : [4, 5] plus [0, 1] gives [4, 6].

Lenormju
  • 4,078
  • 2
  • 8
  • 22