Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 313
Chris G Avatar asked Feb 01 '26 12:02

Chris G


1 Answers

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(',')]
like image 80
Ryan Haining Avatar answered Feb 03 '26 08:02

Ryan Haining



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!