Is it possible to ask the user to write a list as an input such as [1,2,'a','2'] with different data types without the use of any libraries?
Asked
Active
Viewed 53 times
3 Answers
0
You can use eval
but you should understand the caveats. What if the user types in os.system("rm -rf .")
?
In Python 2, input
worked like this (and the "sane" input function was called raw_input
).

tripleee
- 175,061
- 34
- 275
- 318
0
user = []
c = int(0)
while True:
userindent = user.append(input("Type"))
c = c + 1
if c == 3:
break
print(user)
Something like this?

gerrel93
- 89
- 6
0
You can use Python's inbuilt ast
library's literal_eval
to accept python data types as a string and convert them
>>> from ast import literal_eval
>>> user_input = input()
[1,2,'a','2']
>>> user_list = literal_eval(user_input)
>>> user_list
[1, 2, 'a', '2']
OR a simpler version would be
from ast import literal_eval
user_list = literal_eval(input())

Vishnudev Krishnadas
- 10,679
- 2
- 23
- 55