3

(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
  • what do input for input()? it is reading it as a tuple, not a list. Commented Oct 22, 2015 at 23:32
  • @RNar: input is a built-in. Commented Oct 22, 2015 at 23:34
  • 3
    if this is python2, use raw_input instead of input. Please break the problem down before posting. What is the smallest program you can write that still demonstrates your problem? Commented Oct 22, 2015 at 23:34
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. MCVE applies here. Please give us the missing code; input might help, too. Commented Oct 22, 2015 at 23:36
  • 1
    @RyanHaining I have used that before, and apparently it was just a bunch of lucky guesses. Commented Oct 22, 2015 at 23:54

1 Answer 1

7

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(',')]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.