s.chitti - 1 year ago 41

Python Question

Hey I'm trying to find the total average of 3 marks but my structure isn't correct for lines 4 and 9.

So as I have 3 marks to add the v[0], v[1], v[2] represents all 3 marks. Can someone help me to find the average of these marks in line 4 and help with the structure in line 9?

I've tried moving things around but nothings working, therefore here's the code that I have so far.

`def BelowAvgallReport(aDiary): line 1`

sumofallMarks = 0 line 2

for aVal in aDiary.values(): line 4

sumofallMarks = sumofallMarks + aVal v[0] + v[1] + v[2] line 5

avg = sumofallMarks/len(aDiary) line 6

belowallAvgDiction = {} line 7

for k, v in aDiary.items(): line 8

if avg> (v[0] + v[1] + v[2]): line 9

belowallAvgDiction[k] = v line 10

return["Below Avg all Marks:", avg, belowallAvgDiction] line 11

Answer Source

It is apparent that your lines 7-11 have wrong indentation. Otherwise the variable `aDiary`

will be undefined in line7. Also the syntax in line 5 is wrong and `v`

undefined.

Below is the corrected code.
You can use `sum()`

instead of hard coded it.

```
def BelowAvgallReport(aDiary):
sumofallMarks = 0
for aVal in aDiary.values():
sumofallMarks += sum(aVal[:3])
avg = sumofallMarks/len(aDiary)
belowallAvgDiction = {}
for k, v in aDiary.items():
if avg> sum(v[:3]):
belowallAvgDiction[k] = v
return["Below Avg all Marks:", avg, belowallAvgDiction]
```

The second part of your code (for calculation of `belowallAvgDiction`

) can be even simpler:

```
belowallAvgDiction = dict((k,v) for k,v in aDiary.items() if avg> sum(v[:3]))
```