My code works in my IDE but not in Codecademy, please help


#1

Hello everyone,

I am really new to coding, basically just started a few weeks ago. Nonetheless, I've been working through some online tutorials to help me learn, codecademy being one. Through the advice of another website, I downloaded Aptana 3 to use for my IDE. While working through the codes on this website, I found it easier to just work through them on my IDE and copy and paste them into the online once finished.

I have been working on this exercise for a little bit now, and finally got everything to work. I copied and pasted it into the codecademy workspace and it kicks back errors everytime (i.e. when I put get_class_average([lloyd]) it says there is an error with [alice], and when I put get_class_average([alice]) it works fine but pops an error for [lloyd]). Note: as said previously, it works perfectly in my IDE though, no errors and correct results regardless of the variables used.

Below is my code, please run through it and let me know what's up. Also, one additional question as well, the last error I had with this code was the get_class_average function was defined as "def get_class_average(student) but every time I put a call on the function (regardless of the requested variables) it ran the function for everyone in the list "students". So, I was troubleshooting and just decided to through a "s" at the end of student in this line. Needless to say, it works perfectly now. I just don't fully understand why. Can anyone help explain this to 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!
students = [lloyd, alice, tyler]


def average(numbers):
    total = sum(numbers)
    total = float(total)
    total = total / len(numbers)
    return total
    
def get_average(student):
        homework = average(student["homework"])
        quizzes = average(student["quizzes"])
        tests = average(student["tests"])
        avg_score = (homework * 0.1) + (quizzes * 0.30) + (tests * 0.60)
        print student["name"]       #to see who is getting pushed through the code.
        print get_letter_grade(avg_score) 
        return avg_score
        
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"

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

print "outcome of get_class_average(): "
print get_class_average([lloyd])
print
print "outcome of get_average():"
print get_average(lloyd)
print
print "print result[]:"
print result

Summary of questions:
- Why will is this code wrong on Codecademy but not in my IDE?
- Bonus question: Please explain to me why it produced a more reliable result as "students", instead of "student" on my def get_class_average(students) line.

Thank you for your help.


#2

Your get_class_average should look at the argument that is sent to it, so it should be using the name of the parameter in the function header, not some global variable. student and students are different names.

What is it saying is wrong? Perhaps it isn't working any differently for you locally, you just aren't agreeing with Codecademy what your code is supposed to do? Try calling your function multiple times, what happens, and why?


#3

Yes, so, you're saying that the results would be better with the function header as "student" rather than "students"? I just ask because it is a global variable defined as [lloyd, alice, tyler]; so, I thought the same thing and put it as "student" when I first wrote it. But, it is actually giving me better results the other way, and just curious as to why?

The error I am getting is "Oops, try again. get_class_average([alice]) returned 87.6166666667 instead of 91.15 as expected"; however, when I run the code with [alice] on my IDE and in the codecademy it comes up 91.15 without error. But, then just gives me an error of "Oops, try again. get_class_average([lloyd]) returned 87.6166666667 instead of 80.55 as expected", but as I said previously it runs these numbers without fail, something is wrong with codecademy's verification, I think.


#4

Well, does it represent a student or a collection of students? Name things after what they represent.

The global has nothing to do with the function, so it has nothing to do with how you name things in the function.

Call your function multiple times, it should produce the same result every time when given the same input.


#5

I fixed this issue, if anyone is interested, or if you run into this issue again:

Transfer result = [] from the global section and put it in the function get_class_average, as shown below.

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

Sorry if my technical jargon is not quite there yet; I'll get there, lol.