Reputation: 29
The following operation returns -0.20 which is fine:
a=[1.,5]
x=a[0]
y=a[1]
z=(-1*x)/(x*y)
print ("{0:.2f}".format(z))
However, as soon as I read the same data from a file it doesn't work. Why?
TypeError: unsupported operand type(s) for /: 'str' and 'str'
my_file=open("tmp.txt")
for lines in my_file:
x=lines.split()
x0=x[0]
x1=x[1]
print x0,x1
y=(-1*x1)/(x0+x1)
Upvotes: 0
Views: 22
Reputation: 1871
You have to cast the variables to integers:
x0 = int(x[0])
x1 = int(x[1])
In addition, the loop resets the variables x0
and x1
each loop, so the value of y
will be based only on the last line.
Upvotes: 1