Need help fnding the average of a set of numbers in python
in my data file I have
60,66,88,90,44,90,80,77
all the numbers are in one line
this is my code which does not give me the average of my numbers
inFile3 = open("data2.txt","r")
global gradeVar
gradeVar = len(gradeArray)
global Total
Total = 0
fo开发者_JAVA技巧r Numbers in inFile3:
Total = Total + int(Numbers)
inFile3.close()
global averageVar
averageVar = Total/gradeVar
return averageVar
This is the error
Traceback (most recent call last):
File "program.py", line 81, in <module>
main()
File "program.py", line 5, in main
averageVar = Average()
File "program.py", line 39, in Average
Total = Total + int(Numbers)
ValueError: invalid literal for int() with base 10: '60,66,88,90,44,90,80,77\n'
Your problem is here:
for Numbers in inFile3:
Total = Total + int(Numbers)
Numbers
in the code above is a list of lines, rather than a list of numbers.
for Line in inFile3:
for number in Line.split(','):
Total = Total + int(number)
should help.
You also have no need to pre-declare variables the way you are in Python. In fact, doing so with global is positively dangerous unless you know what you're doing and why.
Edit: If you ever have a comma at the end of a line, of a blank value you can change the final line to:
if number.strip():
Total = Total + int(number)
This will ignore any 'empty' number strings that would otherwise throw an error.
This line:
for Numbers in inFile3:
is actually iterating over the lines of the file, not the numbers within each line. You need to iterate over the lines, then for each line split it into numbers, something like this:
for Line in inFile3:
for Number in Line.split(','):
Total = Total + int(Number)
While others have pointed out some of the issues involved in what you are doing, none have pointed out that an average needs not only the sum of all the parts but also the number of all the elements. Hence,
def parseNumberFile(file_name):
for numbers in open(file_name, "r"):
items = numbers.split(',')
yield (len(items), sum(map(int,items)))
which turns it into a generator that you can use as:
total = 0
count = 0
for x,y in parseNumberFile("myData.txt"):
count += x
total += y
average = total/count
Where are you reading the data? You'll need to read it in and then split the String into numbers with something like str.split().
Here is a more pythonic way:
inFile3 = open("data2.txt","r")
grade_list = inFile3.readline()
inFile3.close()
num_list = [int(g) for g in grade_list.split(',')]
average = sum(num_list) / len(num_list)
print average
The error message says it all: '60,66,88,90,44,90,80,77\n'
, considered as a group, is not a valid integer. You need to consider them one by one. First, strip off the new line, then split by the comma.
Change:
for Numbers in inFile3:
To:
# assumes numbers are all on one line with no spaces between
for Numbers in inFile3.read().strip().split(','):
If I had to rewrite from scratch:
from __future__ import division # this import is not needed in python 3
with open('data2.txt', 'r') as f:
numbers = [int(n) for n in f.read().strip().split(',')]
avg = sum(numbers) / len(numbers)
Change
for Numbers in inFile3:
to
for Numbers in inFile3.strip().split(','):
精彩评论