I want to read from stdin five numbers entered as follows:
3, 4, 5, 1, 8
into seperate variables a,b,c,d & e.
How do I do this in python?
I tried this:
import string
a=input()
b=a.split(', ')
for two integers, but it does not work. I get:
Traceback (most recent call last):
File "C:\Users\Desktop\comb.py", line 3, in <module>
b=a.split(', ')
AttributeError: 'tuple' object has no attribute 'split'
How to do this? and suppose I have not a fixed but a variable number n integers. Then?
Use
raw_input()instead ofinput().The misleadingly names
inputfunction does not do what you’d expect it to. It actually evaluates the input from stdin as python code.In your case it turns out that what you then have is a tuple of numbers in
a, all parsed and ready for work, but generally you don’t really want to use this curious side effect. Other inputs can cause any number of things to happen.Incidentally, in Python 3 they fixed this, and now the
inputfunction does what you’d expect.Two more things:
import stringto do simple string manipulations.Unpacking: