AttributeError: 'tuple' object has no attribute 'split' with result of input()?

(This is Homework) Here is what I have:

L1 = list(map(int, input().split(","))) 

I am running into

 File "lab3.py", line 23, in <module> L1 = list(map(int, input().split(","))) AttributeError: 'tuple' object has no attribute 'split' 

what is causing this error?

I am using 1, 2, 3, 4 as input

12

1 Answer

You need to use raw_input instead of input

raw_input().split(",") 

In Python 2, the input() function will try to eval whatever the user enters, the equivalent of eval(raw_input()). When you input a comma separated list of values, it is evaluated as a tuple. Your code then calls split on that tuple:

>>> input().split(',') 1,2 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'split' 

If you want to se that it's actually a tuple:

>>> v = input() 1,3,9 >>> v[0] 1 >>> v[1] 3 >>> v[2] 9 >>> v (1, 3, 9) 

Finally, rather than list and map you'd be better off with a list comprehension

L1 = [int(i) for i in raw_input().split(',')] 
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like