Python 3.5.1 - read multiple inputs into an array -
i'm using python 3.5.1 , running file through command prompt on windows. arguments being passed after program run; ie program prompts input based on generated list.
i'm looking read in multiple numbers on same line separated spaces. python 2.x wouldn't have been issue raw_input proving challenge.
selection = list(map(int,input("enter items archive (1 2 etc):").split(",")))
if enter 2 different numbers on same line:
enter items archive (1 2 etc):29 30 traceback (most recent call last): file "g:\learning\python\need_to_watch.py", line 15, in selection = list(map(int,input("enter items archive (1 2 etc):").split(","))) file "", line 1 29 30 ^ syntaxerror: unexpected eof while parsing
i gave on single line , tried doing in loop different error
data=[] while true: entry = int(input('item number : ')) data.append(entry) if entry == 'q': break
it tries evaluate 'q' variable though haven't eval()'d anything.
this question says use input().split() appear no longer works.... accepting multiple user inputs separated space in python , append them list
i try , catch eof exception, doesn't seem right way it, nor should necessary.
if want pass arguments python script, may want take @ 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 1 2 3 4 [1, 2, 3, 4]
Comments
Post a Comment