error %run

##### Title

##### Question

running file marks.py gives error on line 'math_marks_mean=sum(math_mark)/len'--> 'float' object is not iterable.

code written in marks.py file is==>

math_marks_A=[]

for line in open("student_record.txt"):

fields = line.split(";")

region_code=fields[0]

region_code_stripped=region_code.strip()

math_mark_str=fields[5]

math_mark=float(math_mark_str)

if region_code=="A":

math_marks_A.append(math_mark)

math_marks_mean=sum(math_mark)/len

(math_marks_A)

Print(math_marks_mean)

for line in open("student_record.txt"):

fields = line.split(";")

region_code=fields[0]

region_code_stripped=region_code.strip()

math_mark_str=fields[5]

math_mark=float(math_mark_str)

if region_code=="A":

math_marks_A.append(math_mark)

math_marks_mean=sum(math_mark)/len

(math_marks_A)

Print(math_marks_mean)

Python-3.4.3 Parsing-data 10-11 min 30-40 sec

__Answers:__

The inbuilt sum() function is used to calculate the summation of all integer/float elements in an iterable (list, tuple, etc). You are trying to use the same function on the a single floating point number (math_mark) hence this error shows up.

Please fix your code and the %run command should work fine.

Also, try to avoid similar looking variable names as it is possible that you may be getting confused when using such variables leading to more bugs.

Login to add comment