-2

Is there some way to take a set number of multiple inputs for a list in Python 3.7?

I tried this:

MyList=[int(x) for x in input().split(" ",N)]

when N is the number inputs I want. but whenever I try to run my code I get an error saying:

MyList=[int(x) for x in input().split(" ",N)]
File "<string>", line 1
1 2 3 4 5 6 7
  ^
SyntaxError: invalid syntax

The expected Input is:-

Suppose N=7,

1 2 3 4 5 6 7

This should make it a part of the list

MyList=[1,2,3,4,5,6,7]

yohoo
  • 165
  • 1
  • 2
  • 13

1 Answers1

0

Don't use the N argument to split. If there are more items in the input than this, the remaining ones will all be combined in the last element of the list. But this won't be a valid number, so you'll get an error when it tries to convert that to int.

If you only want the first N elements of the input, use a list slice after splitting.

MyList=[int(x) for x in input().split(" ")[:N]]

It looks like you're using Python 2.x, so you need to use raw_input() instead of input(). See input() error - NameError: name '...' is not defined.

MyList=[int(x) for x in raw_input().split(" ")[:N]]
Barmar
  • 741,623
  • 53
  • 500
  • 612