hi master programmers !

I am new to programming and i have an assignment im working on, it seems simple but i just cant get it ! i was wondering if anyone could help ! it would be greatly appreciated ! here is the question :

===================================
"The sample input is a file of the following format:

ID , Last, First, Lecture, Tutorial,** A1**, A2, A3, A4, A5
10034567, Smith, Winston, L01, T03, 6, 5.5, 8, 10, 8.5
10045678, Lee, Bruce, L02, T05, 4.5, 6.5, 7, 7, 8.5
00305678, Obama, Jack, L01, T05,** 10**, 10, 9, 9.5, 10
00567890, Brown, Palin, L02, T03,** 4**, 7.5, 6.5, 0, 5
10012134, Harper, Ed, L01, T03, 10, 9, 7.5, 10, 6.5
10014549, Johnson, Andrew, L01,T05, 10, 0, 10, 5.5, 7
10020987, Clockwork, Milan, L02, T03, 10, 8.5, 8, 9, 9
10021234, Freeman, Skyski L01, T02, 0, 10, 10, 10, 8.5
EOF

The first line of the file explains each column of the data. Let n be the total number of students, then the next
n lines of the file each corresponds to a student in the class, and contains 10 fields:
(1) Student ID
(2) Last name
(3) First name
(4) Lecture section
(5) Tutorial section
(6-10) Grades for Assignments 1-5
Assuming the grades are stored in a file ‘grades.txt’, then you can read an entire line of the file into a Python string s by using the following Python statements:
file = open (‘grades.txt’, ‘r’)
s = file.readline()
You just need to open the file once, then you can use the readline() function multiple times, to read a successive line each time.
After the n lines of student records, the file ends with a last line that says “EOF”, short for “End of File”. The number n is not known a priori. The sample input doesnt matter it can contain from 100 to 300

students, in a file named ‘grades.txt’. We wish to eventually draw a histogram for grade distribution of Assignment 1. Therefore you need to extract the grade of A1 for each student, by processing his/her corresponding line in the file. Construct a list that will have one entry for each student, stroing his/her A1 grade. Each time you extract a new A1 grade, append it to this list."

so far this is what i got :

file = open('grades.txt','r')
s = file.readline()


for line in file:
     newline = str(line)
     grades = newline.split(",")
     if len(grades)<=4:
         break
     elif len(grades)>5:
         break
     else:
         grades = [float(x) for x in grades]
gradeA1 = grades[5]
print(gradeA1)

but i only get the first grade '6' and not the other A1 grades for any consecutive lines
all the A1 grades should be compiled into a list :(
can anybody help ? i am really stuck ! :(
Thanks !

Hint how many lines you can find that has exactly 4 commas and end up having 5 values in list?

This thread appears to use the same data, and so is probably the same homework problem as http://www.daniweb.com/software-development/python/threads/439504/converting-a-list-to-a-a-string-of-integers
We should perhaps combine them into one.

['ID ', ' Last ', ' First', ' Lecture', ' Tutorial', ' A1', ' A2', ' A3', ' A4', ' A5\n']

In the case of the code in this thread, nothing will happen unless the length of the line read.split() is 5. It will be 10 and I don't think the OP can come up with code to

"hmmm... would i slice the string between the 5th and 6th commas ?".

So should we post a "how to read a file, split the line, and access elements 6 through 10"? They probably should test for first character is a number as well.

Also the following code shows that there is no basic understanding of the different types of data containers which means that hints and partial code will not be enough. Either a poor teacher or somebody didn't pay attention in class.

for line in file:
     newline = str(line)

Edited 4 Years Ago by woooee

Your sample data file has errors in it. Let's assume this is a corrected file:

# uses comma + space as data separator
test_data = '''\
ID, Last, First, Lecture, Tutorial, A1, A2, A3, A4, A5
10034567, Smith, Winston, L01, T03, 6, 5.5, 8, 10, 8.5
10045678, Lee, Bruce, L02, T05, 4.5, 6.5, 7, 7, 8.5
00305678, Obama, Jack, L01, T05, 10, 10, 9, 9.5, 10
00567890, Brown, Palin, L02, T03, 4, 7.5, 6.5, 0, 5
10012134, Harper, Ed, L01, T03, 10, 9, 7.5, 10, 6.5
10014549, Johnson, Andrew, L01, T05, 10, 0, 10, 5.5, 7
10020987, Clockwork, Milan, L02, T03, 10, 8.5, 8, 9, 9
10021234, Freeman, Skyski, L01, T02, 0, 10, 10, 10, 8.5
'''

fname = "grades.txt"
# write the test file
with open(fname, "w") as fout:
    fout.write(test_data)

Now as you read each line (discarding the header), and split it at the comma into a list, assignment1 is at list index 5.

Edited 4 Years Ago by HiHe

This article has been dead for over six months. Start a new discussion instead.