8/9 Oops, try again. get_class_average([lloyd]) returned 85.85 instead of 80.55 as expected ERROR


#1

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]
}

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

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

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

results=[]

def get_class_average(students):
for student in students:
re=get_average(student)
print student
results.append(re)
return average(results)indent preformatted text by 4 spaces


#2

Hi, @anilreddysyamala ,

Because your code is not formatted, it is difficult for other users to read and debug it. After code is been pasted into the editing window for posting, you can format it by selecting it, and then by clicking the </> button above the editing area. This will enable us to see important details, such as the indentation and underscores.

The problem might be with the location of this statement ...

results=[]

Move it to within the get_class_average function, prior to the for loop. There also could be an additional problem with the indentation in your get_class_average function, and other functions, but we cannot see that without the formatting intact. For example, make sure that this line is not indented in a manner that makes it part of the for loop ...

results.append(re)

Check all your functions for issues as described above.


#3

Hi I have the same issue, although I print my results in console:
results: [80.55, 91.14999999999999, 79.9]
average: 83.8666666667
None
however I am getting the error message, could it be that there is a bug in this?


#4

Your Code Should be like this Friend

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]

Add your function below!

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

def get_average(student):
homework = 0.1 * average(student["homework"])
quizzes = 0.3 * average(student["quizzes"])
tests = 0.6 * average(student["tests"])
return homework + quizzes + tests

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

print get_letter_grade(get_average(lloyd))

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


#5

Your Code Should be like this

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]

Add your function below!

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

def get_average(student):
homework = 0.1 * average(student["homework"])
quizzes = 0.3 * average(student["quizzes"])
tests = 0.6 * average(student["tests"])
return homework + quizzes + tests

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

print get_letter_grade(get_average(lloyd))

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


#6

Hi most of the code is boiler plate, the code I am adding looks like this:
def get_class_average(students):
__for student in students:
___value = getaverage(student)
____results.append(value)
____print results
__return average(results)

get_class_average(students)
The under scores are the representation of spaces, I am printing the values this method adds to result list which prints in console:
[80.55, 91.14999999999999, 79.9]
None
Still getting the error message


#7

Had rough times with this one, literally drove me insane.

For sure, that wasn’t the indentation that clogged the code.
Simpler code for get_calss_average , mentioned above just didn’t work.

After changing the variable name in for loop to student and adding seemingly excessive manipulations with calc it finally compiled.

Anyways, here’s the code that definitely worked fine for me:

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]
}
class_list=[lloyd,alice,tyler]
results=[]

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

def get_average(student):
  homework = 0.1 * average(student["homework"])
  quizzes = 0.3 * average(student["quizzes"])
  tests = 0.6 * average(student["tests"])
  return homework + quizzes + tests
  
def get_letter_grade(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(class_list):
  results=[]
  for student in class_list:
    calc=get_average(student)
    calc=float(calc)
    results.append(calc)
  return average(results)    

Still not sure what exactly helped, that could have been CC bug after all