heej heej - 5 months ago 45
Python Question

"TypeError: can only concatenate tuple (not "float") to tuple"

I am writing a program to open and read a txt file and go through loops each line. multiply values in 2nd column and 4th column and assign it to 5th column.

A 500.00 A 84.15 ?

B 648.80 B 77.61 ?

C 342.23 B 39.00 ?

this is part of codes I wrote,

for line in infile:
a,b,c,d = line.split()
e = eval(b) + eval(d)


I kept getting an error saying,

File "C:/Users/hee lim/Desktop/readfile2.py", line 19, in main
e = eval(b) + eval(d)
TypeError: can only concatenate tuple (not "float") to tuple

I covert strings into numbers using "eval" to multiply those numbers.
I don't understand why it flags an error.

thank you for your help.


Looking at this information, I could only tell that the value returned by eval of b and d are of float and tuple type. And you can not do + on float and tuple. For example:

>>> 5.0 + (2 ,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

For debugging, add print line at the start of for loop to see at what value it is raising error.