-
-
Save commadelimited/e634fe30f5589213f103 to your computer and use it in GitHub Desktop.
class StudentAssignments(BaseModel): | |
assignment = ForeignKeyField(Assignment) | |
student = ForeignKeyField(User) | |
grade = IntegerField(null=True) |
# I'm looping over a recordset from my ORM (Peewee). A direct query on the database shows ONLY 5 assignments. Indeed, running assignments.count() also indicates 5 records. But for some reason when this for loop executes, I get an extra 2 records saved to the database. | |
print '-------------------' | |
print assignments.count() # returns 5 | |
for index, assignment in enumerate(assignments): | |
temp = StudentAssignments( | |
assignment=assignment.id, | |
student=student.id, | |
grade=None, | |
) | |
temp.save() | |
print (index, assignment.id, student.id) | |
print '-------------------' | |
Results in this output: | |
------------------- | |
5 | |
(0, 1, 1) | |
(1, 2, 1) | |
(2, 1, 1) | |
(3, 2, 1) | |
(4, 3, 1) | |
(5, 4, 1) | |
(6, 5, 1) | |
------------------- | |
# I'm baffled. I've tried `for index, assignment in enumerate(assignments)` and for assignment in assignments`, both with the same results. Somewhere, somehow, my for loop is being executed additional times. Not sure if this matters, but when the record count is only 1, a single assignment. then I only get 1 record to the database. When it's 2 or 5, I get an extra 2 records to the database. Anyone have any ideas? |
# Now I think it is the ORM doing something. When I do this: | |
for i in range(0,assignments.count()): | |
print dict( | |
assignment=assignments[i].id, | |
student=student.id, | |
grade=None, | |
) | |
#temp = StudentAssignments( | |
# assignment=assignments[i].id, | |
# student=student.id, | |
# grade=None, | |
#) | |
#temp.save() | |
# I get this | |
{'grade': None, 'assignment': 1, 'student': 1} | |
{'grade': None, 'assignment': 2, 'student': 1} | |
{'grade': None, 'assignment': 3, 'student': 1} | |
{'grade': None, 'assignment': 4, 'student': 1} | |
{'grade': None, 'assignment': 5, 'student': 1} | |
# But when I do this: | |
for i in range(0,assignments.count()): | |
print dict( | |
assignment=assignments[i].id, | |
student=student.id, | |
grade=None, | |
) | |
temp = StudentAssignments( | |
assignment=assignments[i].id, | |
student=student.id, | |
grade=None, | |
) | |
temp.save() | |
# I get this: | |
{'grade': None, 'assignment': 1, 'student': 1} | |
{'grade': None, 'assignment': 2, 'student': 1} | |
{'grade': None, 'assignment': 1, 'student': 1} | |
{'grade': None, 'assignment': 2, 'student': 1} | |
{'grade': None, 'assignment': 3, 'student': 1} |
how to use a while loop to cycle through the test grades for a student and calculate the average???
student_info = [['0423456', 'Smith', 'George', 56, 48, 39, 79, 56],
['0232435', 'Jones', 'Larry', 100, 92, 88, 67, 77],
['0142536', 'Nunez', 'Juan', 100, 90, 89, 78, 68],
['0243546', 'Sanchez', 'Maria', 100, 89, 78, 82, 76],
['0314253', 'Adhyan', 'Patel', 75, 95, 65, 72, 67],
['0661398', 'Khan', 'Ahmad', 56, 68, 48, 72, 77],
['0647821', 'Nguyen', 'Cara', 100, 95, 88, 96, 100],
['0872437', 'Jenkins', 'Tamisha', 89, 85, 73, 90, 78]]
Each student in the student_info nexted list contains data for one student in the following format:
['0423456', 'Smith', 'George', 56, 48, 39, 79, 56] is the first student in student_info
For each pass through the "for student in student_info:", the "student" variable is a list that
contains all data for a specific student. The 5 test grades can be found at student[3] to student[7]
The average can be calculated by adding the grades together and dividing by 5.
def add_avg():
for student in student_info:
total = 0; avg = 0.0; grades = []; n=3
#
#
#
#
student.append(avg)
this is the outcome but i don't know how to do while loop
[['0423456', 'Smith', 'George', 56, 48, 39, 79, 56, 55.6],
['0232435', 'Jones', 'Larry', 100, 92, 88, 67, 77, 84.8],
['0142536', 'Nunez', 'Juan', 100, 90, 89, 78, 68, 85.0],
['0243546', 'Sanchez', 'Maria', 100, 89, 78, 82, 76, 85.0],
['0314253', 'Adhyan', 'Patel', 75, 95, 65, 72, 67, 74.8],
['0661398', 'Khan', 'Ahmad', 56, 68, 48, 72, 77, 64.2],
['0647821', 'Nguyen', 'Cara', 100, 95, 88, 96, 100, 95.8],
['0872437', 'Jenkins', 'Tamisha', 89, 85, 73, 90, 78, 83.0]]
please help
In part2.py, if you add right after line 35
count=assignments.count(),
I'm curious if that count is increasing with each loop.