python - A program to read a file and print out the sum of the numbers in the file. The file contains a single floating point numbers separated by commas -


for example, if file contained:

-2.5, 2.0 8.0 100.0, 3.0, 5.1, 3.6 6.5 

then sample run of program like:

please enter file name:  nums.txt sum of numbers 125.7. 

i have run program giving me error, saying "sum_number = sum_number + float(i) valueerror: not convert string float: '.'"

any appreciated!

filename = input("please enter file name: ") sum_number = 0 openthefile = open(filename, "r")  in openthefile:     split = i.split(',')     join = "".join(split)     print(join)  in join:     sum_number = sum_number + float(i)  print("the sum of numbers is",sum_number) 

you can follows:

filename = input("please enter file name: ") sum_number = 0 openthefile = open(filename, "r")  line in openthefile:     num in line.split(','):         sum_number = sum_number + float(num.strip())  print("the sum of numbers %.1f" %(sum_number)) 

we cycle through each line of file, split values on line ,, , add each value on each line our total sum. @ end, print out value.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -