I have a file with several lists of names with scores. Ex: Foo 3 4 5 4 3 80 90 40
I need to split the file and then make it so I can take the numbers and calculate averages, so the numbers need to not be seen by Python as strings. This is what I'm having trouble on. This is the following code I have:

>>> def exchange(grades):
grades[grades.index(min(grades))] = max(grades)
return grades

>>> def average_scores(grades, x, y):
global average
average = 0
count = 0.0
for i in grades[x:y]:
average = average + int(i)
count = count + 1
average = average/count
return average


>>> def change_grades(grades, x):
for i in range(x):
exchange(grades)


>>> def letter_grade(num):
if num < 60:
return "F"
elif num < 70:
return "D"
elif num < 80:
return "C"
elif num < 90:
return "B"
else:
return "A"


>>> def weighted_grade():
quiz_grades = average_scores(grades, 0, 14)
prog_assg = average_scores(grades, 15, 19)
exams = average_scores(grades, 20, 21)
final_exam = grades[22]
participation = grades[23]
weighted_average = quiz_grades * 10 *.3 + prog_Assg * .3 + exams * .2 + final_exam * .2 + participation
num = round(weighted_average)
return num

>>> def main():
global myfile
global myfile2
myfile = open("C:\Python27\input.txt", "rU")
myfile2 = open ("C:\Python27\my.output.txt", "w")
global hold
global hold2
hold = 0
hold2 = []
global grades
grades = []
make_list()
for i in (hold2.split()[0:25]):
grades.append(i)
name = grades.pop(0)
for i in grades:
i = int(i)
times_droped = raw_input("how many grades would you like to change? (use numbers)")
int(times_droped)
change_grades(grades, times_droped)
final_output = []
final_output.append(name)
final_output.append(weighted_grade())
final_output.append(letter_grade())
myfile2.close()
myfile.close()


>>> def make_list():
for line in myfile:
hold = line
hold2.append(hold)
return hold2

>>> if __name__ == '__main__':
main()

With the following error:
Traceback (most recent call last):
File "<pyshell#204>", line 2, in <module>
main()
File "<pyshell#199>", line 13, in main
for i in (hold2.split()[0:25]):
AttributeError: 'list' object has no attribute 'split'

Why on earth the prompt >>> ?
Post indented code from the program file, if code is not experimenting interactively. Push first the code button and ctrl-v the code from clip board.

Just take out the global statements and use the return values from function for side effects. Alternatively you can modify the values of items of list passed in. Then maybe we can track down the bugs.

Member Avatar for nabla2

Suppose we have a file foo.txt:

Foo 3 4 5 4 3 80 90 40
Bar 5 6 7 8 9 10 11 12
with open('foo.txt') as f:
    for line in f:
        foo = map(float, line.split()[1:])
        average = sum(foo) / len(foo)
        print average

@nabla2: Better not to give ready solutions, OP does not learn much that way. Your solution is subtle in that is uses float in line 3, in order to avoid getting problem with integer division at line 4. map is nice (old Lisper myself), but usually now it is preferable to use generator or list comprehension.

pythonic data ;)

Spam 3 4 5 4 3 80 90 40
Eggs 5 6 7 8 9 10 11 12

for i in (hold2.split()[0:25]):
AttributeError: 'list' object has no attribute 'split'

You want to split, etc. "i", not the list "hold2".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.