# [Tutor] I've run into a jam on the exercise on file I/O

Nathan Pinno falcon3166 at hotmail.com
Wed Aug 3 20:59:53 CEST 2005

```Here is the latest code and error:
Traceback (most recent call last):
File "D:\Python24\grades.py", line 99, in -toplevel-
out_file.write(x+","+max_points[x]+"\n")
TypeError: unsupported operand type(s) for +: 'int' and 'str'

max_points = [25,25,50,25,100]
assignments = ['hw ch 1','hw ch 2','quiz   ','hw ch 3','test']
students = {1:max_points}

print "2. Remove student"
print "9. Exit"

print '\t',
for i in range(len(assignments)):
print assignments[1],'\t',
print
keys = students.keys()
keys.sort()
for x in keys:
print x,'\t',

print

def choice():

def school():
return int(raw_input("Student ID: "))

in_file = open(filename, "r")
while 1:
if in_line == "":
break
in_line = in_line[:-1]
[students,max_points] = string.split(in_line,",")
in_file.close()

out_file = open(filename, "w")
for x in students.keys():
out_file.write(x+","+max_points[x]+"\n")
out_file.close

while 1:
ID = school()
students[ID] = [0]*len(max_points)
print "Remove student"
ID = school()
if students.has_key(ID):
del students[ID]
else:

ID = school()
if students.has_key(ID):
print "Type in the number of the grade to record"
print "Type in a 0 (zero) to exit"
for i in range(len(assignments)):
print i+1,' ',assignments[i],'\t',
print
which = 1234
while which != -1:
which = int(raw_input("Change which Grade: "))
which = which-1
if 0 <= which < len(grades):
elif which != -1:
else:
filename = raw_input("Filename to load: ")
filename = raw_input("Filename to save: ")
break
else:
print "That's not a choice!"
print "Goodbye."

How can I fix it?
----- Original Message -----
From: "Nathan Pinno" <falcon3166 at hotmail.com>
To: "Alan G" <alan.gauld at freenet.co.uk>; "Yoo, Danny"
Cc: "Tutor" <tutor at python.org>
Sent: Wednesday, August 03, 2005 12:42 PM
Subject: Re: [Tutor] I've run into a jam on the exercise on file I/O

> I've been chatting with Albertito on MSN and came up with this solution:
> int(x), and change student into student ids, and remove #Max. Will this
> work, or will I need to add sequential IDs?
> ----- Original Message -----
> From: "Alan G" <alan.gauld at freenet.co.uk>
> To: "Nathan Pinno" <falcon3166 at hotmail.com>; "Yoo, Danny"
> Cc: "Tutor" <tutor at python.org>
> Sent: Tuesday, August 02, 2005 1:43 AM
> Subject: Re: [Tutor] I've run into a jam on the exercise on file I/O
>
>
>>
>>> 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?
>>
>> Its not necessary for what you are doing but it's how you'd normally do
>> it
>> in a relational database, so if you ever needed to turn your file based
>> program into a full blown database then using IDs would actually help!
>>
>> The other thing you could do is hold all the data for a single student
>> in one structure. Thus instead of having lots of lists all linked by
>> a common index (or ID) you have one collection of students (probably
>> a dictionary keyed by name) each with all of its own data. This
>> involves a more complex data structure and is a move in the direction
>> of object oriented programming but without actual classes and objects
>> being involved. This might also allow you to use the shelve moduile
>> to save/restore your data to file easily.
>>
>> Just a thought,
>>
>> Alan G.
>>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
```