Need help with python dialing tool

in my data file i have

60,66,88,90,44,90,80,77

all numbers are on the same line

this is my code that doesn't give me the average of my numbers

inFile3 = open("data2.txt","r") global gradeVar gradeVar = len(gradeArray) global Total Total = 0 for Numbers in inFile3: Total = Total + int(Numbers) inFile3.close() global averageVar averageVar = Total/gradeVar return averageVar 

This is mistake

 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' 
+4
source share
6 answers

Your problem is here:

 for Numbers in inFile3: Total = Total + int(Numbers) 

Numbers in the above code is a list of strings, not a list of numbers.

 for Line in inFile3: for number in Line.split(','): Total = Total + int(number) 

should help.

You also do not need to pre-declare variables the way you are in Python. In fact, doing this with the global is positively dangerous if you do not know what you are doing and why.

Edit: If you ever have a comma at the end of a line, an empty value, you can change the final line to:

  if number.strip(): Total = Total + int(number) 

This ignores any "empty" strings of numbers that otherwise cause an error.

+5
source

This line:

 for Numbers in inFile3: 

actually iterates over the lines of the file, not the numbers inside each line. You need to iterate over the lines, then for each line divide it into numbers, something like this:

 for Line in inFile3: for Number in Line.split(','): Total = Total + int(Number) 
+2
source

While others have pointed out some of the problems associated with what you are doing, no one has indicated that, on average, not only the sum of all parts, but also the number of all elements is required. 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, which you can use as:

 total = 0 count = 0 for x,y in parseNumberFile("myData.txt"): count += x total += y average = total/count 
+2
source

Where do you read the data? You will need to read it and then split the string into numbers using 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 
+1
source

Everything is indicated in the error message: '60,66,88,90,44,90,80,77\n' , considered as a group, is not a valid integer. You must consider them one by one. Separate the new line first, then separate it with a comma.

Edit:

 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) 
+1
source

Change

 for Numbers in inFile3: 

to

 for Numbers in inFile3.strip().split(','): 
0
source

Source: https://habr.com/ru/post/1341304/


All Articles