Help needed in proceeding to the next level


#1



https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/4?curriculum_id=4f89dab3d788890003000096

I am currently learning python on codecademy.com and am not able to proceed to the next level. Please help me! This is the question that codecademy.com has posed me with:

  1. Define a function called get_class_average that has one argument students. You can expect students to be a list containing your three students.
  2. First, make an empty list called results.
  3. For each student item in the class list, calculate get_average(student) and then call results.append() with that result.
  4. Finally, return the result of calling average() with results.

And this is the code I have written: -

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

Add your function below!

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 (0.1*homework)+(0.3*quizzes)+(0.6*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'

get_letter_grade(get_average(lloyd))

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

When I try to execute the code, it gives me this error message: "Oops, try again. Make sure you have defined get_class_average"

I am having a problem in the part of the code enclosed between the two '***'s. I am not very sure about the syntax. Could you please check it? Also I feel there is some problem with the question as the function get_average accepts dictionaries as arguments but the questions is asking me to do something else. I am confused and am not able to proceed further.



#2

Hey @prabhatikavij,

Try to put results inside of your function body...


#3

Thank you so much @bartholomewallen for your help! I tried doing it but it doesn't work.


#4

@prabhatikavij you should make these following changes: 1. In the instructions they have asked you to put empty results[] after defining the function ..so it will be like this
def get_class_average(students):
results = []
for student in students:
results.append(get_average(student))
return average(results)

  1. previously they would have asked to calculate the grade and print it out for each student, not just lloyd. so you have to simply put it as print for each student
    print get_letter_grade(get_average(student)) or put it for each of the student individually like so:

print get_letter_grade(get_average(lloyd))
print get_letter_grade(get_average(alice))
print get_letter_grade(get_average(tyler))

Hope this solves for you ! I recently went through this..Good luck!

@bartholomewallen helped me out ! lol


#5

Thank you so much for your replies, @bartholomewallen and @asher3667. I have finally managed to find the error in my code and process to the next level. The error in my code was that the line 'results=[]' should have been inside the function body (as suggested by both of you!) and there was a silly mistake too! There was a problem with the indentation of the function.

Thank you so much for your replies. I really appreciate the fact that you gave your time to answer my doubt.