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:

global average
average = 0
count = 0.0
average = average + int(i)
count = count + 1
average = average/count
return average

for i in range(x):

if num < 60:
return "F"
elif num < 70:
return "D"
elif num < 80:
return "C"
elif num < 90:
return "B"
else:
return "A"

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 = []
make_list()
for i in (hold2.split()[0:25]):
i = int(i)
times_droped = raw_input("how many grades would you like to change? (use numbers)")
int(times_droped)
final_output = []
final_output.append(name)
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'

4
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by woooee

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.

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

Edited by nabla2: n/a

@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".

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.