[Tutor] I've run into a jam on the exercise on file I/O
Nathan Pinno
falcon3166 at hotmail.com
Tue Aug 2 01:46:40 CEST 2005
What if I were to use ID's for the students and use the ID's as the sequence
index, and link the students and their grades to the IDs?
Just a suggestion,
Nathan Pinno
----- Original Message -----
From: "Danny Yoo" <dyoo at hkn.eecs.berkeley.edu>
To: "Nathan Pinno" <falcon3166 at hotmail.com>
Cc: "Tutor" <tutor at python.org>
Sent: Monday, August 01, 2005 3:03 PM
Subject: Re: [Tutor] I've run into a jam on the exercise on file I/O
>
>
> On Mon, 1 Aug 2005, Nathan Pinno wrote:
>
>> I've seem to run into a jam while working on the exercise on file I/O.
>> Here's the error:
>> Filename to save: university.txt
>> Traceback (most recent call last):
>> File "D:\Python22\grades.py", line 99, in ?
>> save_grades(students,filename)
>> File "D:\Python22\grades.py", line 51, in save_grades
>> out_file.write(x+","+max_points[x]+"\n")
>> TypeError: sequence index must be integer
>
>
> Hi Nathan,
>
>
> I'll try to capture what I think of when I see error messages like this,
> to better illustrate how to take something like this, and puzzle it
> through.
>
>
> Let's take a close look at the error message, first:
>
>> TypeError: sequence index must be integer
>
> Python doesn't like some place where we're doing an indexing operation.
> Where would that be?
>
>
> Let's look at the line that Python pointed out, around line 99.
>
>> out_file.write(x+","+max_points[x]+"\n")
>
> The only indexing operation I can see here is the subexpression:
>
> max_points[x]
>
> and the error makes sense if if 'x' isn't a number. Let's take a look at
> where 'x' is being assigned, and see if we're assigning it to a
> non-number.
>
>
>> def save_grades(students,filename):
>> out_file = open(filename, "w")
>> for x in students.keys():
>> out_file.write(x+","+max_points[x]+"\n")
>> out_file.close
>
> Ok, 'x' comes from the keys of our 'students' dictionary. Now our problem
> turns into: are the keys of the students dictionary numbers, or are they
> something else?
>
>
> At a high level, we're trying to determine: is our use of max_points[x] in
> the save_grades() function the thing that's broken, or is the value 'x'
> that's broken?
>
> For the moment, we'll follow 'x', and if that turns out ok, then we should
> go back to save_grades() and see what it's trying to write out. (We can
> do things in the other order, too, of course. Flip a coin. *grin*)
>
>
> Let's look at how 'students' is being constructed, since the values of 'x'
> comes from the keys of the 'students' dictionary.
>
> ######
>> max_points = [25,25,50,25,100]
>> assignments = ['hw ch 1','hw ch 2','quiz ','hw ch 3','test']
>> students = {'#Max':max_points}
> ######
>
> Ok. We certainly see that one of the keys in the 'students' dictionary
> isn't a number, so at least we can confirm the error message. '#Max', for
> example, is certainly not a number.
>
>
> What is 'students' supposed to represent? That is, can we try describing
> what the keys are supposed to be, and what the values are supposed to be?
>
> I will guess that you're thinking of it more like a table, with:
>
> #Max | [25, 25, 50, 25, 100]
> ---------+-----------------------
> Danny | [20, 20, 40, 20, 90]
> Nick | [25, 25, 49, 24, 99]
>
>
> where the keys are student names, and the values are their respective test
> scores. It might be good to rename 'students' to 'student_grades' then,
> to make that relationship more clear.
>
>
> Anyway, in that case, the 'students' dictionary actually looks fine: it
> contains the right thing. Ok, let's go back to the definition of
> save_grades():
>
>> def save_grades(students,filename):
>> out_file = open(filename, "w")
>> for x in students.keys():
>> out_file.write(x+","+max_points[x]+"\n")
>> out_file.close
>
> and, for the moment, let's completely ignore the body of the function, and
> just imagine: what do we imagine should happen when we save the grades to
> disk? It should read off the students (the "student to grades")
> dictionary, and write it to disk.
>
> Let's look back at the body of save_grades() now. How does max_points
> relate to writing out all of the students and their grades to disk? Does
> it really matter, or did we mean to use a different value?
>
>
>
> Please feel free to ask questions on any point of this. Good luck!
>
>
More information about the Tutor
mailing list