[Tutor] Reading from files problem
Chris Castillo
ctcast at gmail.com
Tue Apr 21 04:01:20 CEST 2009
this worked for me fine.
#Author - Chris Castillo
#Created 4/19/09
#Program 6
import time
#Open and read text file with student information
gradesfile = open("grades.dat", "r")
time_acc = time.asctime()
#Ask user for student ID to lookup student information
sought_id = raw_input("Please enter a student identifier to look up grades:
")
#Assign values for each students' info in list
for line in gradesfile:
lines = line.split(",")
student_id = lines[0]
last_name = lines[1]
first_name = lines[2]
major = lines[3]
hw1 = lines[4]
hw2 = lines[5]
hw3 = lines[6]
hw4 = lines[7]
hw5 = lines[8]
hw6 = lines[9]
hw7 = lines[10]
test1 = lines[11]
test2 = lines[12]
test3 = lines[13]
#Arithmetic operations for all students' grades
total_points = 550
hw_grades = float(hw1) + float(hw2) + float(hw3) + float(hw4) +
float(hw5) + float(hw6) + float(hw7)
test_grades = float(test1) + float(test2) + float(test3)
student_points = float(hw_grades) + float(test_grades)
avg_grade = round(float(student_points / total_points) * 100.0, 2)
#Assign grades to letter values
if avg_grade < 60:
letter_grade="F"
elif avg_grade < 70:
letter_grade="D"
elif avg_grade < 80:
letter_grade="C"
elif avg_grade < 90:
letter_grade="B"
elif avg_grade < 101:
letter_grade="A"
else:
print avg_grade, "is not valid"
#variable for output to request.dat text file
req_output = student_id + ", " + last_name + ", " + first_name + ", " +
time_acc
#Check to see if student id number is in the list of student id numbers
if lines[0] == sought_id:
new_file = open("requests.dat", "w+")
new_file.write(req_output)
print "\n", last_name + ", "+ first_name + ", " + student_id + ", "
+ major
print "\n", time_acc
print "\nExams - ", test1 + ", " + test2 + ", " + test3
print "Homework - ", hw1, ", ",hw2, ", ", hw3, ", ", hw4, ",", hw5,
", ", hw6, ", ", hw7
print "\nTotal points earned - ", student_points
print "\nGrade: ", avg_grade, "that is a ", letter_grade
gradesfile.close()
hope this helps someone. I know it's probably not the most efficient way to
handle this kind of problem but It works nonetheless.
On Mon, Apr 20, 2009 at 8:24 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:
>
> "Scott SA" <pydev at rscorp.ab.ca> wrote
>
> May have been a bad assumption on my part as I envisioned pickling a
>> dict. and that just got too complicated.
>>
>
> A pickled dict?
> That would be a shelve mebbe?
>
>
> PS. My email is acting up, did my prev. message actually make it to the
>> list?
>>
>
> Yes, I saw it just after sending mine...
>
> Alan G
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090420/5b67ca92/attachment-0001.htm>
More information about the Tutor
mailing list