Tuesday, May 21, 2013

Learning Python(Pt II)


lloyd = {
    "name": "Lloyd",
    "homework": [90, 97, 75, 92],
    "quizzes": [88, 40, 94],
    "tests": [75, 90]
}
alice = {
    "name": "Alice",
    "homework": [100, 92, 98, 100],
    "quizzes": [82, 83, 91],
    "tests": [89, 97]
}
tyler = {
    "name": "Tyler",
    "homework": [0, 87, 75, 22],
    "quizzes": [0, 75, 78],
    "tests": [100, 100]
}

students = [lloyd, alice, tyler]

def average(x):
    return sum(x) / len(x)
def get_average(x):
    return (average(x["homework"]) * 0.1 +
    average(x["quizzes"]) * 0.3 +
    average(x["tests"]) * 0.6)

def get_letter_grade(score):
    score = round(score)
    
    if score >= 90:
        return "A"
    elif 80 <= score < 90:
        return "B"
    elif 70 <= score < 80:
        return "C"
    elif 60 <= score < 70:
        return "D"
    elif score < 60:
        return "F"
    
print get_letter_grade(get_average(lloyd))

def get_class_average(students):
    n = 0
    for each in students:
        n+=get_average(each)
    return n/len(students)
    
print get_class_average(students)

Here is some of the code I've been doing on codecademy. Basically this code consists of 3 dictionaries containing students grades, then there are 4 functions that do different things such as: finding the average grade for a student, finding the weighted average for the student, then finding the letter grade, then finding the overall letter grade. Python is actually not that difficult compared to javascript.

No comments:

Post a Comment