8. Part of a Whole / Troubleshooting


#1

Stuck on this error message...

  File "python", line 50
    return average(results)
         ^
SyntaxError: invalid syntax

Why is this an invalid syntax when it's defined in my code below?

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)
    total = total/len(numbers)
    return total

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

print get_class_average(students)
print get_letter_grade(get_class_average(students))

#2

Python indicates the error ^ at the point where it cannot execute a statement. That means the error is before that point.

Check the bracket count on the previous line.


#3

Thank you! Needed a fresh pair of eyes.....

I'm getting a new error message now -- apparently I'm not calling the right definition of the function.

Traceback (most recent call last):
  File "python", line 52, in <module>
NameError: name 'students' is not defined

Any idea what I should be calling instead? I've tried both below with no luck...

get_class_average(s) & get_class_average(student)

Updated code:

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)
    total = total/len(numbers)
    return total

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

print get_class_average(students)
print get_letter_grade(get_class_average(students))

#4

Just under the dictionaries, add the students list (referred in the instructions as class list)...

students = [ lloyd, alice, tyler ]

We wrote this in lesson 3, I believe, but for some reason it doesn't get carried forward.


#5

Thanks so much for the explanation @mtf.

I've added the aggregated student list but the average calculated is incorrect. Doesn't seem to make sense since I've passed all previous chapters with correct inputs. Not sure where I'm wrong...

Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected

My call function prints the following, which doesn't match the error message either:

80.55
B
None

Revised code:

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)
    total = total/len(numbers)
    return total

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

print get_class_average(students)
print get_letter_grade(get_class_average(students))

Thank you for going through this with me!


#6

Too much indentation. Back off four spaces so it lines up with for and does not return until the loop is completed iteration.


#7

Dingdingding solved! Omgsh thank you!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.