I want to accept the input as lists. Is there a way like int(input())
to do this?
I want to accept the input in []
:
Enter list1: [80, 60, 70]
Is it possible to do this without using strip()
or split()
?
CodePudding user response:
Can you be more specific with what you are trying to accomplish?
Code from first answer without .split() method.
numbers = list(map(int, input()))
print(numbers)
Whatever you type or pass to input will be collected by input. Without knowing what you're end goal is, it's hard to tell how to help. Maybe this helps?
>>> input()
[10, 20, 30]
'[10, 20, 30]'
>>> input()
10
'10'
>>>
CodePudding user response:
split is probably the best way in most cases but it's not the only way.
exec("list = " input())
would be another and would expect a list formatted like you said.
why not split? are you struggling to format the input? you could conditionally slice off the first and last characters or use regex to remove all non numerical characters.
CodePudding user response:
You can not use the int(input()) method to initializate the input.
Here is the way using the split()
The input is:
80 60 70
Then you modify into the list:
numbers = list(map(int, input().split()))
print(numbers)
Your output is:
[80, 60, 70]