Python 3.5.1 - Read Multiple Inputs Into An Array
I'm using python 3.5.1 and running my file through command prompt on windows. The arguments are being passed after the program is run; ie the program prompts for input based on a p
Solution 1:
If you want to pass arguments to a python script, you may want to take a look at argparse instead: https://docs.python.org/3/library/argparse.html
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('integers', type=int, nargs='+')
args = parser.parse_args()
print(args.integers)
python script.py 1234
[1, 2, 3, 4]
Solution 2:
entry = input('Enter items: ')
entry = entry.split(' ')
entry = list(map(int, entry))
print(entry)
Or more concisely:
entry = list(map(int, input('Enter items: ').split(' ')))
print(entry)
Post a Comment for "Python 3.5.1 - Read Multiple Inputs Into An Array"