I am currently going through Codecademy's Python course (I don't come from a coding background but I'm thoroughly enjoying it) and I got to the end of one of the sections and thought, "Well it worked, but that seemed wildly inefficient." I tried shortening unnecessary parts and came up with 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]
}
classlist = [lloyd, alice, tyler]
# Add your function below!
def get_average(student):
average_homework = sum(student["homework"]) / len(student["homework"])
average_quizzes = sum(student["quizzes"]) / len(student["quizzes"])
average_tests = sum(student["tests"]) / len(student["tests"])
average_total = average_homework * 0.1 + average_quizzes * 0.3 + average_tests * 0.6
return average_total
def get_letter_grade(score):
if score >= 90:
return "A"
if score < 90 and score >= 80:
return "B"
if score < 80 and score >= 70:
return "C"
if score < 70 and score >= 60:
return "D"
else:
return "F"
def get_class_average(classlist):
a = []
for student in classlist:
a.append(get_average(student))
return sum(a) / len(a)
get_class_average(classlist)
Now this bit works, but I was wondering if there are more places I could trim the code down without losing the functionality (I'm guessing there is in the get_average function but the things I tried came back as errors because of the "name" field in the dictionary)? Codecademy seems good for learning the fundamentals, but I wanted to get some insight into the "best" way to do things. Should I not worry about reworking functional code to be more efficient at first and just learn the fundamentals, or should I keep trying to make the "best" code as I learn?