A thanks Tony, it worked, i initialized the variable at the top
On Monday, October 30, 2017 at 3:18:06 AM UTC+2, Anthony wrote:
>
>
>
> On Sunday, October 29, 2017 at 2:24:47 AM UTC-4, mostwanted wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *I AM TRYING TO CALCULATE THE AVARAGE IN AMY VIEW, I ADDED A VARIABLE
>> CALLED average TO HOLD THE RESULTS OF THE AVAERAGE CALCULATIONS BUT I KEEP
>> GETTING THIS ERROR:<type 'exceptions.NameError'> name 'average' is not
>> definedI DONT UNDERSTAND WHAT I'M DOING WRONG!!!HERE IS MY CODE:{{extend
>> 'layout.html'}}..............................<table><tr> <th>Term</th>
>> <th>Subject</th> <th>Mark Acquired</th> <th>Total Mark</th>
>> <th>(%) Acquired</th> <th>Grade</th> <th>Teacher's
>> Comment</th></tr>{{for report in form:
>> perc=(float(report.marks)/float(report.total))*float(100) if
>> perc>=75: grade='A' elif perc>=65: grade='B'
>> elif perc>=55: grade='C' elif perc>=45:
>> grade='D'#THE CALCULATION FOR AVARAGE IS DONE HERE average+=perc*
>>
>
> You cannot increment a variable without first initializing it somewhere.
> Before the for loop starts, you should have something like:
>
> average = 0
>
> More generally, I would say this is too much logic in the view. You should
> probably calculate the grades and average in the controller (or via virtual
> fields if reports are DAL records).
>
> Also, be careful with code like report.subject.subject_name inside a loop
> -- if that is a recursive select on a DAL record, it will generate an
> additional database query for every single record. You might be better off
> with a join to get all the related data in a single query.
>
> Anthony
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.