Where is the problem, looks identical!

#1

Hi,

I have two codes, both logically are the same and to some extent written the same but one works and the other doesn't, i just want to understand why doesn't mine work, i know I'm writing it a bit different (we all do) but it should work. any help please?

the issue is not in the indentation.

MyCode (it didn't work):_

``````lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}

students=[lloyd, alice, tyler]

def average(numbers):
total = float(sum(numbers))
result = total / len(numbers)
return result

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
total = (homework*.1) + (quizzes*.3)+(tests * .6)

score = int(score)
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"

def get_class_average(students):
results=[]
for student in students:
i=get_average(student)
results = results.append(i)
print results

print get_class_average(students)

TheSecond_Code_ (it worked) :_

``````lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
students = [lloyd, tyler, alice]

def average(numbers):
return float(sum(numbers))/len(numbers)

def get_average(student):
homework = average(student["homework"]) * .1
quizzes = average(student["quizzes"]) * .3
tests = average(student["tests"]) * .6

return homework + quizzes + tests

if score >= 90:
return "A"
elif score >=80:
return "B"
elif score >=70:
return "C"
elif score >=60:
return "D"
else:
return "F"

def get_class_average(students):
results = []
for i in students:
results.append(get_average(i))
return average(results)

print get_class_average(students)

#2

#3

understood, but why? where is the problem with the code?

#4

because of what i just said? the exercise expects get_class_average to `return` the `average()` of result.

you do this in your second code, but not in your first

#5

I just updated it to return the average as such :

``````def get_class_average(students):
results=[]
for student in students:
i=get_average(student)
results = results.append(i)
return average(results)``````

Still not working, here is the error :

Traceback (most recent call last):
File "/Users/Documents/workbench.py", line 56, in
print get_class_average(students)
File "/Users/Documents/workbench.py", line 54, in get_class_average
return average(results)
File "/Users/Documents/workbench.py", line 23, in average
total = float(sum(numbers))
TypeError: 'NoneType' object is not iterable

#6

`append()` will update the list for you, so all you have to is:

``results.append(i)``

because append will update the list for you, you don't need update the variable as well.

also, a function ends the moment a return keyword is reached, this currently happens in the first iteration of your loop. You might want to change the indent of return so the whole for loop can run

#7

not sure why, but the return was for the entire "For statement", and it was on the script too, but seems like when i copied and pasted i messed it up.

However, for some reason, when i removed the results variable and left the results.append(i) as is, it is working working now!

you are awesome man, thank you for taking the time to explain!

#8