Part of the whole


#1

This is my code, I do not understand what is wrong with it, please help 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]
}

Add your function below!

def average(numbers):
total=float(sum(numbers))
result=total/float(len(numbers))
return result
def get_average (student):
summa = 0
for key in student:
if key == "homework":
summa += average(student ["homework"]) * 0.1
elif key == "quizzes":
summa += average(student ["quizzes"]) * 0.3
elif key == "tests":
summa += average(student ["tests"]) * 0.6
return summa

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"

students=[lloyd,alice,tyler]
def get_class_average(students):
results=[]
for student in students:
results.append(get_average(students))
return average(results)



Replace this line with your code.


#2

For best results, follow the instructions to the letter. This way you get the most benefit without things getting compllicated (or redundant). Consider that length is a counting number, so we should leave it as a counting number. The numerator is a float, so the quotient will be a float.

  • Define a function called average that has one argument, numbers.
  • Inside that function, call the built-in sum() function with the numbers list as a parameter. Store the result in a variable called total.
  • Like the example above, use float() to convert total and store the result in total.
  • Divide total by the length of the numbers list. Use the built-in len() function to calculate that.
  • Return that result.

                                   # step
def average(numbers):              # 1
    total = sum(numbers)           # 2
    total = float(total)           # 3
    result = total / len(numbers)  # 4
    return result                  # 5

This is how to avoid problems. Sure we can simplify this and reduce the amount of code, but it is not what the instructions ask for.

In "Weight and See" we are given an example, and an explanation:

The \ character is a continuation character. The following line is considered a continuation of the current line.

This would indicate that now is the perfect time to practice with that aspect of the lesson.

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

Again, there are ways we can write this differently, but as I so often repeat, follow the instructions and take something away from the lesson. After we have passed the module would be the time to come back to review and experiment with ways to refine the code.

Now we get to the simple error that is causing a problem:

Did you mean to write,

    results.append(get_average(student))

Sorry if this post sounds like a lecture. It's not meant that way, only as advice to help prevent confusion and help you get the most from the lessons.


#3

thanks a lot, it helped :slight_smile:


#4

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