Skip to content

Instantly share code, notes, and snippets.

@4rshdeep
Created April 27, 2017 12:41
Show Gist options
  • Save 4rshdeep/52b357d81fceb9e8ece8211484aaa65d to your computer and use it in GitHub Desktop.
Save 4rshdeep/52b357d81fceb9e8ece8211484aaa65d to your computer and use it in GitHub Desktop.
Want to get average of marks in any minor or a quiz just copy that data in a .txt file and then run the following program. And it somehow prints the average. Make sure that you have just one .txt file in the current directory
import os
#gets the files ending with .txt in the current directory
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
if f.endswith('.txt'):
filename = f
#opens the file
a = open( filename , 'r')
#lines contains list of lines in the file
lines = a.readlines()
#strips the lines of the trailing whitespace
lines = [x.strip() for x in lines]
totalLines = len(lines)
# to get the average
x = 0
y = 0
for line in range(totalLines):
words = lines[line].split(" ")
#words is now list of words in a line
lastValue = words[len(words)-1]
#lastValue is the last value in a line and this is the logic behind this program as it captures the last value which is usually the total value
if lastValue!="A":
x += float(lastValue)
y +=1
if(y!=0):
print(x/y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment