0

Below is my code:

 n = int(input())

 for i in range(n):

    d = int(input())

    for i in range(d):
        a = list(map(int,input().split()))
    print(a)

But I want my code to do this, for example: Suppose d = 4 is my user input Now, I want (random example) a = [1, 4 , 8, 19]. That too from user input. This is an example of the output of. I am not able to frame the question properly. So, here is the desired output:

3 #input for n

2 #input for d

[4,6] #output for a

5 #input for 2nd d

[4,2,1,4,7] #output for a, 5 elements as d = 5

2 #input for last d, as 'n' was 3

[1,9] #(the last array a)

2 Answers2

0

Your code can be simplified. It seems like you are trying to combine the 2 very different methods of collecting data below. You should choose one or the other.

Individual entries

n = int(input('How many numbers?\n'))

res = []
for i in range(1, n+1):
    res.append(int(input('Enter number{0}\n'.format(i))))

Simultaneous entries

n = int(input('How many numbers?\n'))

in_str = input('Enter the numbers separated by space:\n')

res = list(map(int, in_str.split()))
jpp
  • 159,742
  • 34
  • 281
  • 339
0

Problems with your code

  • you are using same i variable in both for loop
  • a = list(map(int,input().split())) scans whole line, so no need of for loop

Solution

n = int(input())

for i in range(n):

    d = int(input())

    a = list(map(int,input().split()))
    print(a)

Output

2
5
1 2 3 4 5
[1, 2, 3, 4, 5]  # Output
2   
1 2
[1, 2]  # output

If you got your answer please accept it else comment What's the problem?

PSKP
  • 1,178
  • 14
  • 28