Reputation: 91
How do I add the sum of the squares in the input file?
Input file is a txt file and is shown below:
10 9 8 7 1 2 3 4
3 -4 1 -2
0.743 -12.3 5.3333 3
-3.3
Hence the output may look like this;
324.0
30.0
189.28613789000002
10.889999999999999
I can't add the floating numbers using sum as it displays an error, so any help would be much appreciated.
Here is my code:
#Ask the user to input a file name
file_name=input("Enter the Filename: ")
#Opening the desired file to read the content
infile=open(file_name,'r')
#Importing the math library
import math
#Iterating for the number of lines in the file
for line in infile:
#Converting the file to a list row by row
line_str=line.split()
for element in range(len(line_str)):
line_str[element]=float(line_str[element])
line_str[element]=math.pow(line_str[element],2)
total=sum(line_str[0:len(element)])
print(total)
Upvotes: 0
Views: 13245
Reputation: 2619
I think you are misunderstanding the split function and what it does.
$ python
Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> line = "10 9 8 7 1 2 3 4"
>>> line_str = line.split()
>>> line_str
['10', '9', '8', '7', '1', '2', '3', '4']
split takes the string and at every space splits the string into substrings, each substring is an element in the list
Your loop code is behaving like you need to find the individual numbers in the string.
>>> for element in range(len(line_str)):
... line_str[element]=float(line_str[element])
... line_str[element]=math.pow(line_str[element],2)
... total=sum(line_str[0:len(element)])
... print(total)
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
TypeError: object of type 'int' has no len()
So now lets loop through the list (using enumerate as suggested above) and convert each element to a float, square it using math.pow and store it back in the same list. The you can pass the list to sum() to get your total. The beauty of Python is that you can always drop down into the interpreter and run your commands one by one and investigate the results.
>>> import math
>>> line = "10 9 8 7 1 2 3 4"
>>> line_str = line.split()
>>> line_str
['10', '9', '8', '7', '1', '2', '3', '4']
>>> for i,value in enumerate(line_str):
... fvalue = float(value)
... line_str[i] = math.pow(fvalue,2)
...
>>> line_str
[100.0, 81.0, 64.0, 49.0, 1.0, 4.0, 9.0, 16.0]
>>> sum(line_str)
324.0
>>>
Now the question you may one day ask yourself is, why did I want to use math.pow(fvalue,2)
when I could have just done fvalue**2
or even fvalue*fvalue
When you are ready you can read about it here Exponentials in python x.**y vs math.pow(x, y)
Upvotes: 0
Reputation: 1884
for line in infile:
numbers = map(float, line.split())
squares = (i ** 2 for i in numbers)
print(sum(squares))
Upvotes: 1
Reputation: 76792
There are multiple problems with your code.
The error you got is because you do:
total=sum(line_str[0:len(element)])
and element is an integer as it loops over the the values range()
generates.
You have the total
isation and print in the for loops. You call print
for every element, not for every line, so you cannot get the output that you need.
This: line_str[0:0]
will get you an empty list, and your use of this: line_str[0:element]
will never include the last element
This is a version that works with minimal changes:
#Ask the user to input a file name
# file_name=input("Enter the Filename: ") # temporary commented out for easier testing.
file_name = "input.txt"
#Opening the desired file to read the content
infile=open(file_name,'r')
#Importing the math library
import math
#Iterating for the number of lines in the file
for line in infile:
#Converting the file to a list row by row
line_str=line.split()
for element in range(len(line_str)):
line_str[element]=float(line_str[element])
line_str[element]=math.pow(line_str[element],2)
total=sum(line_str[0:element+1])
print(total)
This gives:
324.0
30.0
189.28613789000002
10.889999999999999
but can be much improved upon:
=
in assignmentsfor .... range(somelist):
and then index somelist
. Instead use enumerate.Something along these lines:
# Ask the user to input a file name
# file_name=input("Enter the Filename: ")
file_name = "input.txt"
# Opening the desired file to read the content
infile=open(file_name,'r')
# Importing the math library
import math
# Iterating for the number of lines in the file
for line in infile:
# Converting the file to a list row by row and convert to float
line_str = [float(x) for x in line.split()]
for idx, element in enumerate(line_str):
line_str[idx] = math.pow(element,2)
total = sum(line_str)
print(total)
Upvotes: 2