I need to input 3 integers form stdin and outut their sum. Here’s what i do:
(n,l,k) = raw_input().split()
print n + l + k
Of course, i get n,l,k string concatenation, so i change this a little:
(n,l,k) = raw_input().split()
print int(n) + int(l) + int(k)
Now it’s ok. But is there any way to set data type for the variables ‘on the fly’, so n,l,k became int whitout explicit reassigning for further use( i think of n = int(n) ) ?
UPDATE:
What if i need to input int, string, int (tuple items are of different types) ?
THis means i can’t do
(n,S,k) = [int(i) for i in raw_input().split()]
Any zip(), lambdas, accepting types?
Thanks in advance,
Ilya
Not really. You need to tell it what you want. There are some more succinct ways to do that in Python.
This is generally true of all user input. It must be validated and converted to what the rest of your program expects.
Oh, well you can use the type objects to list the types you want. Use the fact that variables are references and you can reference a type object with a variable as well.