Home > Enterprise >  because input does not work when executed in a for loop
because input does not work when executed in a for loop

Time:08-03

my intention is to execute an input N number of times by using a for loop. However, the code I wrote doesn't behave the way I expect it to.

Here I share my code:

N, K = map(int, input().split())

values ​​= (list(map(int, input().split()))[1:] for _ in range(N))
print(values)

When I execute the code the program ends without executing the inputs. Also, when I try to print the output, I get the following output:

<generator object <genexpr> at 0x000001D040ED03C0>

Here is a sample of how the program should work:

SampleInput

3 1000
2 5 4
3 7 8 9
5 5 7 8 9 10

in this case, N would be equal to 3, therefore 3 inputs should be executed

CodePudding user response:

Just fix your code like this

values ​​= [list(map(int, input().split()))[1:] for _ in range(N)]

Instead of list comprehension, you can for in values like this

for i in values:
    print(i)
  • Related