python - How do I allow a user to input vectors rather than strings? -


i'm trying make program allow user input vectors of form (x,y,z) using python's built in input() function.

if entered in python out using input() function indexes each vector separately. example,

>>> z = (1,2,3), (4,5,6), (7,8,9) >>> z[1] (4, 5, 6) 

but when try use input function run following problem.

>>> z = input('what vectors?  ') vectors?  (1,2,3), (4,5,6), (7,8,9) >>> z[1] '1' 

why using input function turn string , there way around this?

thanks

in python 3, input returns string. need convert string. type of input recommend using liter_eval module ast:

import ast vectors = ast.literal_eval('(1,2,3), (4,5,6), (7,8,9)') vectors[1]     #(4, 5, 6) 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -