I want to do the following:
import sys
for line in sys.stdin:
a,b,c,d=line.split(" ")
e=(a+b+c+d)/6
print(a,b,c,d,e)
I want a
, b
, c
and d
to be integers or float to be able to operate on e
, I can put float(a), float(b), etc... but it is very laborious, is there a way to declare the variable as float, integer or whatever ?
I try to do:
float(a), float(b), float(b), float,(d)=line.split(" ")
a, b, c, d = float(line.split(" ")
Which is not correct. Doing the following I have no problems.
import sys
for line in sys.stdin:
e = 0.0
for res in line.split(" "):
e = float(res) + e
print(e)
But it seems to me many lines to be able to capture only a few numbers.
What method can I use to be able to capture the data?
In C I would do it like this:
float a, b, c, d;
scanf("%f%f%f%f", &a, &b, &c, &d);
On the line where you assign the values you could use a comprehension or you can use
map
. For example, withmap
would be: