You are splitting b1
on single spaces, and this can lead to empty values:
>>> '88 89 '.split(' ')
['88', '', '89', '']
It is the extra empty strings here that cause int()
to throw an exception:
>>> int('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
Use str.split()
with no argument instead; extra whitespace is then stripped:
>>> '88 89 '.split()
['88', '89']
You have some other problems as well in your code. Take a good look at:
def lab8():
userinput= "Lab8.txt"
lenoffile= len(userinput)
print "There is", lenoffile, "lines"
File= open (userinput, "r")
studentscores1= File.read()
studentlist= studentscores1.split("\n")
return studentlist, lenoffile
Here, lenoffile
is not the number of lines in the file. It is the number of characters in 'Lab8.txt'
; both values happen to be 8
, but add or remove some lines from that file and the number will be wrong for the rest of your code.
If you are supposed to keep these numbers together with the names and write out the calculations again, you'll have to do some work keeping the names together.
Here is an alternative version to solve the same task:
outputfile = raw_input("What is the output filename? :")
with open('Lab8.txt') as infile, open(outputfile, 'w') as outtfile:
for name in infile:
scores = next(infile).split() # next() grabs the next line from infile here
scores = map(int, scores)
quiztotal = sum(scores[:4]) / 5
midtermtotal = sum(scores[5:7]) / 2
finaltotal = scores[7]
score = quiztotal * .3 + midtermtotal * .4 + finaltotal * .3
outfile.write(name)
outfile.write('{0:0.2f}\n'.format(score))