Profile image of marisbest2
Submitted by marisbest2
about 13 years

Having trouble with this lesson? Think your average() function is correct? Check out this post!

Many people on this lesson are having a similar problem, so I am writing a short post here to better guide people to pass this lesson.

The lesson asks you to write a function average which will take a list of numbers as input and return the average value in that list. Many people seem to think that they are supposed to find each student’s average in this function. Thus, many people have a variation of the following code:

def average(student):
    total = 0
    for subject in student:
        total += student[grade]
    return total/len(student)

Alternatively, some people get the average of each subject and try to return that like so:

def average(student):
    total = 0
    for subject in student:
        total += sum(student[subject])/len(student[subject])
    return total/len(student)

Now obviously these are just two examples of many different solutions people try. But they are representative of the logic of many people. I will now try to explain where this logic is flawed.

Let us start with the def statement. I have noticed many people write, as I have above def average(student). This tells me that people are incorrectly assuming that average will be passed a dictionary/student and/or that this function should return a student’s average. Neither of these are the case. The correct def statement looks something like def average(list_of_numbers). average will be passed a list of numbers, not a dict or student.

Other errors stem from similarly faulty logic, and by misunderstanding what the lesson is asking you to do. This lesson is asking you to make a general function that will function anywhere and in any context in which we need to find an average value in a list of numbers. In a later lesson, you will use this function to find the grade average of each student. But for now we are making a generalized function for any situation.

Here are some valid inputs and outputs for average()

average([1]) #1.0
average([0,2]) #1.0
average([1,2]) #1.5

If your code works for all three of those inputs, you should be fine.

Cheers and hope this helps!

Answer 517fc2552d0d61f61b0034ab

278 votes

Permalink

Its not so much a problem with the logic of students as a miscommunication error on the part of the lesson writer. Misunderstanding the problem is not the same as having faulty logic. But thank you for clearing up what the lesson is asking us to do.

Profile image of hotdogontology
Submitted by hotdogontology
about 13 years

45 comments

Profile image of stgeorge
Submitted by stgeorge
about 13 years

I agree on this. Just take a look at Adam W.Cooper’s other lessons and you will see the pattern being repeated. The fact that so many of us have problems with his lessons, means something is definitively wrong with the way those lessons are taught.

Profile image of marisbest2
Submitted by marisbest2
about 13 years

can you provide more specific examples so we can maybe work on them?

Profile image of stgeorge
Submitted by stgeorge
about 13 years

Can you tell me how to quote the code in this comment box? When I add a new answer I simply use the “{}” but in what should I do in here?

Profile image of stgeorge
Submitted by stgeorge
about 13 years

From: Just average 04/05:

“Write a function called get_class_average that takes your student list as input to compute the average of your entire class.”

What student list? We have dictionaries with student names, not lists.

Profile image of marisbest2
Submitted by marisbest2
about 13 years

Sadly, you cannot format code in the Q&A comments (yet…) And good example. Thanks.

Profile image of stgeorge
Submitted by stgeorge
about 13 years

No, thank you. For all the effort an explanation you gave us.

Profile image of brad0247
Submitted by brad0247
almost 13 years

We did make a student list, though. It just got deleted as we completed exercises. Remember, it was students = [lloyd, alice, tyler]. I believe for this exercise we need to add the list back in so we can use it as an input for the get_class_average function.

Profile image of e4Nf3Bb5
Submitted by e4Nf3Bb5
almost 13 years

I would ditto stgeorge’s initial comment. I was moving along just fine through the lessons until hitting the Cooper series. I think there are two problems with some of the lessons - ambiguous wording and a need for iterative practice with syntax. I found myself continually having to go back and reference the list and dictionary syntax examples because too much of the code is directly given to you, and then you are asked to repeat it wholesale. As to ambiguity, another sample to add to this one: A Day at the Supermarket, section three “Shopping Trip” uses ambiguous wording (“Write a function compute_bill that takes a parameter food as input and computes your bill by looping through your food list and summing the costs of each item in the list.” - what list? The groceries list? Some other ‘food’ list that’s getting added in? What’s the point of the grocery list otherwise? In implementation it ends up making very little difference,yes, but it’s that kind of wording that pops up in other places as well where the teachers intentions are a bit vague or the phrases don’t match directly.

Profile image of bitRunner35068
Submitted by bitRunner35068
almost 13 years

I agree with all the critic on Cooper’s lessons. following the lessons of weinstein or others was perfekt but when doing cooper’s its alsways results in totaly unneccessary murder of time. e4Nf3Bb5 and stgeorge found the best words - i encountered the same problems. beeing able to look up examples of right solutions instead of searching in this forum would be great.

Profile image of tagBlaster16192
Submitted by tagBlaster16192
almost 13 years

I agree with the comments above

Profile image of elizan
Submitted by elizan
almost 13 years

I agree that some of the directions are vague and miscommunicate what the lesson actually wants. I never even finished the “Shopping Trip” because it would not work no matter what I did, even using the code others said worked for them. I asked friends who know python and they saw nothing wrong with my code, so I just moved on. Clarifying the directions might be a good idea.

Profile image of 9182
Submitted by 9182
almost 13 years

I also agree

Profile image of gri3g0
Submitted by gri3g0
over 12 years

I dont think it is a miscommunication error. It says to define a function that returns sum(x) / len(x) BUT Python needs to know that the result is a float, so the hint tells us how to make it: return float(sum(x)) / len(x)

Profile image of goallee
Submitted by goallee
over 12 years

I receive an error that the average function doesn’t return a float, but it does. I ran my code in IDLE and it is returning a float. Any suggestions?

Profile image of goallee
Submitted by goallee
over 12 years

Of course, I’m also getting the correct answer for test cases and a few others that I’ve made up.

Profile image of blogRockstar09731
Submitted by blogRockstar09731
over 12 years

What worked for me is where you initially set the total variable to 0, set it to 0.0 instead

Profile image of digitalAce74847
Submitted by digitalAce74847
over 12 years

Thanks Chris! I had this same problem and your comment fixed it

Profile image of CryptoCoinSolutions
Submitted by CryptoCoinSolutions
about 12 years

I see in the instructions: “write a function average that takes a list of numbers as the parameter lst (for list)”

If I follow the instructions:

def average(1st)

I’m going to totally ignore this. Again, the architect of a program is the first point of failure.

Profile image of etherised
Submitted by etherised
about 12 years

since i don’t see a way to upvote comments, i would like to echo the sentiments regarding the lessons by Adam Cooper. i find the instructions to be worded in an unnecessarily confusing manner, and the inconsistencies in the code i’ve written being replaced / modified only adds to the confusion, as it breaks any continuity of thought as i (try to) progress.

Profile image of agrabloj
Submitted by agrabloj
almost 12 years

my thoughts exactly

Profile image of reactor84
Submitted by reactor84
almost 12 years

I also agree.

Profile image of blogRunner07279
Submitted by blogRunner07279
almost 12 years

I often find myself coming here and having to find the answer because of how confusing the lesson can be. It’s counter productive and I barely learn anything by doing so. I get the basics, come to completely baffling point in the lesson with no idea how to proceed and then have to come to the Q&A forum for help. The “hints” are also something to be desired, they provide no insight into what common problems might be.

Profile image of harry93rose
Submitted by harry93rose
over 11 years

I completely agree, this is a difficult enough process without the questions being delivered in riddles.

Profile image of pancakerz
Submitted by pancakerz
over 11 years

Speak for yourselves. The average function was easy enough to figure out if you just read the instructions rather than try to write it with preconceived assumptions.

Profile image of systemWhiz29530
Submitted by systemWhiz29530
over 11 years

def average(listofnumbers): return sum(listofnumbers)/len(listofnumbers)

followed by function calls would suffice , since we are passing a list to the function

Profile image of jufranzoi
Submitted by jufranzoi
over 11 years

def avarage(numbers): total=sum(numbers) total=float(total) return total / len(numbers)
this is what the text tells you to do but i still get an error …

Profile image of CptJHarkness
Submitted by CptJHarkness
over 11 years

I agree. The instructions are a list of 5 things to do to while the actual task only takes 3 steps. Many of us are seeing it the way it says just like jufranzoi pointed out. For it to be logical it would need to be instructed more like 1.Define a function called average with an argument of numbers. 2. steps 2, 3, and 4 can be placed here with some minor editing 3. Return the result. That would make much more sense logically. When you tell me to store something in a var called total then to alter it then to alter it again I’m going to end up with total in the script more than one time. No offense the the instructors because I really appreciate the fact that I can learn all this for free and from home but it seems like we skipped alot of basics, and since this is a basics course…..yeah.

Profile image of alegionofgeniusgmail.com
over 11 years

Definitely concur, there are more than a few issues like this I’ve seen in the instructions thus far.

Profile image of JeffGeorge
Submitted by JeffGeorge
over 11 years

The instructions in “Student Becomes the Teacher 8/9: Part of the Whole” are deeply confusing, and almost caused me to give up on the entire Python course at Codecademy. To begin: the sentence “You can expect STUDENTS to be a list containing your three students” does not mean the same thing to a beginner as “Create a list called STUDENTS, containing lloyd, alice, and tyler,” but that is what the author intends for us to do. Next, he refers to a CLASS list, which seems to mean the STUDENTS list he never quite told you to create, under a different name, but “class” is a reserved term in Python, for declaring classes, so naming your list of students “class” won’t work. And he never suggests putting in PRINT commands to actually see what your code does - though that’s the only source of mission-accomplished satisfaction we’re going to get out of these simple programs. And as other commenters have mentioned, there is way too little practice with list and dictionary syntax before you are expected to have it memorized, and to double back and refer to examples prior lessons in Codecademy requires difficult navigation and lots of “Open link in new tab” so you don’t lose your place in the current lesson. I’ve done a lot of Codecademy lessons and courses, but this is the first I’ve encountered that frustrated me nearly to the point of quitting. If I hadn’t found the advice in the Q&A forum, I would have abandoned Codecademy’s Python course with this lesson.

Profile image of digitalWhiz34506
Submitted by digitalWhiz34506
over 11 years

I just looked at this and it had 193 votes on it. Read through and it was amazing, so i voted too. :)

Profile image of greenlancer
Submitted by greenlancer
over 11 years

There needs to be a serious review of Adam Cooper’s work. I’ve gone through the HTML/CSS track, JS track, and most of Python with few problems. I get here and it is nothing but cognitive dissonance, poorly worded instructions, shoddy hints if there were any, and endless frustration trying to use material I haven’t had enough practice with. Thank DOG for google and these forums, otherwise I would have quit.

Profile image of -_________-
Submitted by -_________-
over 11 years

I did think there was something wrong with how some of the lessons were worded. For everyone’s sakes, I would hope that all these errors are corrected to prevent so much confusion.

Profile image of anonymous
Submitted by anonymous
about 11 years

Well Connor, I think you forget that you wouldn’t even have this free source of information about coding, if it wasn’t for Adam Cooper

Profile image of digitalWhiz34506
Submitted by digitalWhiz34506
about 11 years

Connor, you need to lighten up a bit. You have been able to code this far and now you decide to call Adam Cooper names. What’s the point? To be honest, the only reason why you dislike Adam is because you can’t figure it out. This is not Adam’s fault. Other people have been able to pass this, so you can as well. Stop being such a negative dishearten person.

Profile image of ajaxAce97175
Submitted by ajaxAce97175
about 11 years

I got through the lesson just fine. The only problem is that he failed to give clear concise instructions. Other students got through it because they copied someones source..

Profile image of -_________-
Submitted by -_________-
about 11 years

I’d actually consider myself guilty of that…

Profile image of 455nefer
Submitted by 455nefer
about 11 years

First off, just to be cynical, he’s the teacher, not us. He should be teaching us, not ignoring us as we try to teach him. Idea: why not email a survey form about each lesson for feedback once you complete it?

Profile image of astarrh
Submitted by astarrh
about 11 years

Some people have no respect

Profile image of methodSlayer52421
Submitted by methodSlayer52421
almost 11 years

has nothing to do with respect, the lesson writer admittedly throws us under the bus and asks us to do things that we’re still(obviously) not comfortable with by telling us outright that they’ve been holding our hands and are no longer going to do so. to be honest, i started struggling with my comprehension of the lessons shortly after the grocery shopping….i’ve been going back over the lessons several times to try and understand what the teacher is asking for, i didn’t get it until today.

Profile image of Sam.I.Am01
Submitted by Sam.I.Am01
almost 11 years

@Shane Stout exactly ^^^

Profile image of GangstaCoder
Submitted by GangstaCoder
almost 11 years

ditto

Profile image of anonymous
Submitted by anonymous
almost 11 years

It was not clear from the instructions and it became marginally clearer with this elaboration. Only when I studied other people’s code below it made real sense to me.

Profile image of tagMaster60392
Submitted by tagMaster60392
over 10 years

I agree with all of the above. Some of the instructions need to be rewritten. If a few students don’t understand the instructions, then it’s a problem with the students. If a lot, or even most, of the students don’t understand, then it’s a problem with the instructions.

Profile image of ajaxJumper27573
Submitted by ajaxJumper27573
over 10 years

I couldn’t agree less… I am appreciative that the lesson wasn’t spelled out, such that all I had to do was cut and paste. With the current lesson, I believe I now have a much better understanding. Seeing how to do something incorrectly allows the student to see the problem from many different angles, which adds to their base knowledge.

Profile image of BobGears
Submitted by BobGears
over 10 years

If the instructions were perfect, we wouldn’t even be having this conversation. “Food” can’t come out of nowhere.

Answer 536047f67c82ca03a70003b8

134 votes

Permalink

hi, there. here is my code, you can try, it works

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))
    return total/len(numbers)
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    
    return 0.1 * average(student["homework"]) + 0.3 * average(student["quizzes"]) + 0.6 * average(student["tests"])

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"
        
print get_letter_grade(get_average(lloyd))

def get_class_average(students):
    results = []
    for item in students:
        studentAvg = get_average(item)
        results.append(studentAvg)
    return average(results)

students = [lloyd,alice,tyler]
print get_class_average(students)
print get_letter_grade(get_class_average(students))
Profile image of Msdam0n
Submitted by Msdam0n
about 12 years

73 comments

Profile image of cloudPro35074
Submitted by cloudPro35074
about 12 years

Hi, thanks, it works, I finally did, but I really I don’t understand how it works because this code is similar to mine

Profile image of leetk321
Submitted by leetk321
almost 12 years

Thank you very much :)

Profile image of Gundam101
Submitted by Gundam101
almost 12 years

thanks so much

Profile image of Mutembei
Submitted by Mutembei
almost 12 years

Thanks a lot. I had done everything I could but got stuck on the very last line. Yours shed a large photon of light on that, thanks again.

Profile image of arsalanj123
Submitted by arsalanj123
almost 12 years

Thanks

Profile image of pmporter
Submitted by pmporter
almost 12 years

Thanks! I got the correct answer, but used the wrong approach. I explicitly named the students in function:

print get_class_average([alice, lloyd, tyler]) print get_letter_grade(get_class_average([lloyd, alice, tyler]))

…rather than creating a list: students = [lloyd, alice, tyler]

I was going to try that approach next (honest)..your solution made that moot…

Thanks again.

Profile image of doctor_boy
Submitted by doctor_boy
almost 12 years

Thanks!

Profile image of akin.okon
Submitted by akin.okon
almost 12 years

I was stuck at this point for up to a day, but after seeing the way you defined your get_class_average function, realized it was all in the instructions, just not clear enough. Thanks for helping with this. Now I can proceed. :-)

Profile image of microPro74181
Submitted by microPro74181
almost 12 years

thanks a lot..i was stuck here for a day…finally i can understand now.. :

Profile image of yebuoy
Submitted by yebuoy
almost 12 years

it worked! thanks

Profile image of anonymous
Submitted by anonymous
almost 12 years

can someone PLEASE EXPLAIN, what python is doing from the SECOND DEF, def get_average(student): homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”])

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

can someone please explain why he/she uses student as the parameter, I mean I don’t see a list or a variable called student.

Profile image of digitalWhiz48379
Submitted by digitalWhiz48379
almost 12 years

The variable student is first created in the def function line. The variable is created to be used only inside the function. The variable can be anything you want. The student variable is used in the three average calculations for homework, quizzes, and tests. When you call the function with the parameter lloyd with get_average(lloyd), it assigns lloyd to the variable student inside the function. Everywhere that the variable student is found within the function is replaced with lloyd. Please let me know if this makes sense.

Profile image of anonymous
Submitted by anonymous
almost 12 years

cheers andrew m8 that is a much clear

Profile image of frank1701a
Submitted by frank1701a
almost 12 years

Thank You, Thank You! I was stuck on this for some time. I think though there is an error in the compiler here, at one point I reset the code and ran it to submit (with nothing done for 8/9) and I was getting the same error about alice not being a str or int. I had to log out of codeacademy come back in and I submitted an empty 8/9 and then it told me I had to define the function.

Profile image of gigaPlayer42102
Submitted by gigaPlayer42102
almost 12 years

I had a similar approach bet was writting “student in students” instead of “item in students”. Somehow this had a confliction with a previous variable assignment maybe? One thing: there seems to be a disagreement on where in this code to place the students = [“””] list. After the dictionaries, withing the last definition or at the end outside of the definition?

Profile image of ceebs
Submitted by ceebs
almost 12 years

Thank you for this. I was pulling my hair out on the last step of this one. I had apparently missed the step asking me to make the list “students” somewhere. When I created this list used a different list name and it kept spitting out errors despite the console output being right. I change the list name and update the final print commands with the new list name and it passes me.

Profile image of ClearSkies
Submitted by ClearSkies
over 11 years

A big thank you for the help… it was a confusing journey to the next level.

Profile image of Yalama
Submitted by Yalama
over 11 years

i am sorry people but for me it doent work….. i did the same

Profile image of mohan22
Submitted by mohan22
over 11 years

works perfect, the part i was missing in the get_class_average() function was the: return AVERAGE(results) instead I had just return results which gave me a list of each student’s average instead of just 1 number for the whole class. misunderstood directions on this step of the lesson

Profile image of Rashid-Malik
Submitted by Rashid-Malik
over 11 years

Yea, this works

Profile image of luismocampo
Submitted by luismocampo
over 11 years

This way works:

return 0.1 * average(student[“homework”]) + 0.3 * average(student[“quizzes”]) + 0.6 * average(student[“tests”])

but this also worked for me:

hw = homework * .10 qzs = quizzes * .30 tst = tests * .60 return hw + qzs + tst its more simplified

Profile image of patrascual
Submitted by patrascual
over 11 years

Thanks!

Profile image of femke11
Submitted by femke11
over 11 years

thank you so much!!

Profile image of tradefutures
Submitted by tradefutures
over 11 years

you know more than the instructor. Thank you very much for the clarification!

Profile image of anonymous
Submitted by anonymous
over 11 years

Oh thanks so much! I get stuck there and cannot explain why

Profile image of cssSolver27656
Submitted by cssSolver27656
over 11 years

Thanks, although I was getting the correct result in the interpreter, it didn’t like the way I had gotten it!

Profile image of SteffintheElite
Submitted by SteffintheElite
over 11 years

you are literally a legend

Profile image of SteffintheElite
Submitted by SteffintheElite
over 11 years

votes for u. :)

Profile image of igsm_
Submitted by igsm_
over 11 years

I dont get why I need variables (homework etc.) ?!

Profile image of LegoBatmanMaster
Submitted by LegoBatmanMaster
over 11 years

YOU ARE… DA BEST.

Profile image of LegoBatmanMaster
Submitted by LegoBatmanMaster
over 11 years

YYYYYYYAAAAAAASSSSSSS!!!!!!!

Profile image of blogCoder96253
Submitted by blogCoder96253
over 11 years

Great, but why define variables in get_average function if you don’t use them later?

Profile image of manndaw11
Submitted by manndaw11
over 11 years

in get_average function: you said: return 0.1 * average(student[“homework”]) + 0.3 * average(student[“quizzes”]) + 0.6 * average(student[“tests”])

you already set: average(student[“homework”]) to the variable homework

so it is more efficient to return .1*homework

Profile image of megaAce20365
Submitted by megaAce20365
over 11 years

I have been trying to get this to work for hours. Thank you so much for posting your results!!

Profile image of scriptMaster02019
Submitted by scriptMaster02019
over 11 years

thnak you working..:)

Profile image of Vyachez
Submitted by Vyachez
over 11 years

Oh, finally, thank you!

Profile image of navneet7988
Submitted by navneet7988
over 11 years

thanks!!!

Profile image of Illusione
Submitted by Illusione
over 11 years

thank you!!

Profile image of Adz65
Submitted by Adz65
over 11 years

thanks

Profile image of greenlancer
Submitted by greenlancer
over 11 years

Can anyone explain why I need an empty results list? results = []? I was just told to put it in, and don’t know why I’d ever use it.

Profile image of arrayPro37412
Submitted by arrayPro37412
over 11 years

Thanks..I did not have my average function coded correctly. When I looked at the average function mathatically it made sense.

Profile image of anonymous
Submitted by anonymous
over 11 years

Diego Villena, you append different forms of data to the results list, so when you call the new average() function, it’ll work on all of the items in the results list.

Profile image of dolsen446
Submitted by dolsen446
about 11 years

Hallelujah! Several days of blood sweat and tears and now I finally have an answer. I’ll have to look at this code to see where I was flawed but I’m glad I can finally continue with the course now.

Profile image of viresh92
Submitted by viresh92
about 11 years

thenx dear

Profile image of code180
Submitted by code180
about 11 years

Thank you, I can go to bed now.

Profile image of anonymous
Submitted by anonymous
about 11 years

thank you…:)

Profile image of digitalRockstar05331
about 11 years

o my god! Same as mine and i searched the mistake arround 2 hours! it was the last return positioned 1 tab to the right! DAMN! :D

Profile image of j_mazolenyyahoo.com
Submitted by j_mazolenyyahoo.com
about 11 years

Thank you

Profile image of devBlaster47925
Submitted by devBlaster47925
about 11 years

thank you)) it works

Profile image of arrayRockstar44264
Submitted by arrayRockstar44264
about 11 years

Ha that return in get_class_average caught me out too. Moved it out of the for loop and it all worked, Thanks

Profile image of scriptAce95101
Submitted by scriptAce95101
about 11 years

you did well but this part “return 0.1 * average(student[“homework”]) + 0.3 * average(student[“quizzes”]) + 0.6 * average(student[“tests”])” needs to be revised. Think for a moment.. You set those values equal to variables named homework, quizzes, and tests. Essentially you stored them in a variable for no reason because in your return you typed the contents of the variables you initialized. The code is functional but it does not meet the conceptual parameters. its should just look like this “def get_average(student): homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”]) return (homework * .10 + quizzes * .30 + tests * .60)”

Profile image of scriptRockstar47655
Submitted by scriptRockstar47655
about 11 years

Thanks.this solved my problem.

Profile image of Rishabh_Sahrawat
Submitted by Rishabh_Sahrawat
about 11 years

last line was creating error, now it got resloved. Thanks man (y) :)

Profile image of AriThereYet
Submitted by AriThereYet
about 11 years

Reducing redundancy, 一只特立独行的猫 in your code you could simply remove the homework, quizzes, tests variable and leave the return statement, it will give the result. And Brian Blanchard, his answer is perfect but you don’t need parenthesis around your return statement.

Profile image of ElenaCodeGirl
Submitted by ElenaCodeGirl
about 11 years

Yes indeed, it works! It seems that the get_average() function - was the problem in my case, so my version looked like this:

ef get_average(student):
homework= average(student[“homework”]) quizzes= average(student[“quizzes”]) tests = average(student[“tests”]) return 0.1homework + 0.3quizzes + 0.6*tests

Profile image of xfalcon_
Submitted by xfalcon_
about 11 years

thakyou :) saved me ALOT of time

Profile image of objectPro43977
Submitted by objectPro43977
about 11 years

Change: return 0.1 * average(student[“homework”]) + 0.3 * averag \

Profile image of Abhinandanudupa
Submitted by Abhinandanudupa
about 11 years

Thank you a lot you helped me through out the chapter!!!!!:)

Profile image of RowanStewart
Submitted by RowanStewart
almost 11 years

Hi guys, thought this might help: works for me :) def average(numbers): total = sum(numbers) total = float(total) total = total/len(numbers) return total

Profile image of budf
Submitted by budf
almost 11 years

Thank you!!!! Nice work.

Profile image of smyles21
Submitted by smyles21
almost 11 years

not sure what they problem is but I get the same result as your code which is 83.8666667 and have tried both your code and mine and I get this error message. “ Oops, try again. get_class_average([alice]) returned 85.6875 instead of 91.15 as expected”

So neither of these amounts correspond to the result 83.66667. Anyone got any suggestions?

Profile image of Pinzonc
Submitted by Pinzonc
almost 11 years

why don’t you do print average(get_class_average(students))

Profile image of tagSurfer71658
Submitted by tagSurfer71658
almost 11 years

def get_average(student): homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”])

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

You calculate these values twice, once in your declarations, and again when you return.

Profile image of Adandreu09
Submitted by Adandreu09
almost 11 years

My problem was forgetting to summate the totals. That was it.

Profile image of MonikasSandbox
Submitted by MonikasSandbox
almost 11 years

Thanks for this code!! I got confused at the last part, and I don’t think I could have progressed past this without being able to see your answer and compare it to mine. On another note, once I’d figured out my code error I had another persistent problem where it was giving me a wrong average. Turns out the last line was indented one too many times, haha. Don’t forget to double-check your indents people!

Profile image of pyMaster19782
Submitted by pyMaster19782
almost 11 years

Thanks! So glad to get past this idiotic lesson!

Profile image of aomcastor
Submitted by aomcastor
almost 11 years

Man, I just got confused after many functions defined, lol.

Profile image of AgniHe
Submitted by AgniHe
almost 11 years

Thank you for your post! I have one question about below code: return 0.1 * average(student[“homework”]) + 0.3 * average(student[“quizzes”]) + 0.6 * average(student[“tests”])

As you have stored the value into homework, quizzes and tests, why can’t you use them directly in this code?

I hope I am clear with my question, thank you.

Profile image of Fred444
Submitted by Fred444
almost 11 years

Yes, He, that is right, you just need ‘return 0.1 * homework….’

Profile image of cloudJumper97222
Submitted by cloudJumper97222
almost 11 years

I tried using average = get_class_average(students) print average but it gives an error , why ?

Profile image of CGumbs77
Submitted by CGumbs77
over 10 years

I used : def get_average(student): homework = average(student[“homework”]) * 0.1 quizzes = average(student[“quizzes”]) * 0.30 tests = average(student[“tests”]) * 0.60 return homework + quizzes + tests

and it worked.

just seemed like cleaner code and less lines used.

Profile image of Ihaveafork
Submitted by Ihaveafork
over 10 years

return 0.1 * homework + 0.3 * quizzes + 0.6 * tests —-this is what i used as i had already saved all the averages in variables i felt no need to write them out all over again

Profile image of esraaelkady
Submitted by esraaelkady
over 10 years

thank you soooo much

Answer 5296320d548c35c4c8000428

51 votes

Permalink

is it just me or does this guy love to throw in completely new concepts and not explain them at all?

Profile image of themoose00
Submitted by themoose00
over 12 years

11 comments

Profile image of codePro15753
Submitted by codePro15753
over 12 years

well… I don’t know about you, but we all have Google…. and I am not trying to be a dick… but if you really want to learn Python, then read the lesson, and then google things you don’t get… even if there are some differences between 2.7 and version 3, you are doing yourself a far greater service by a little extracurricular reading/researching.

Profile image of coreJumper71318
Submitted by coreJumper71318
about 12 years

^^ this guy, I like this guy ^^

Profile image of d8aninja
Submitted by d8aninja
almost 12 years

^^seconded

Profile image of lumogas
Submitted by lumogas
over 11 years

Yes, extracurricular activity helps, yes, looking stuff up on google helps too, but having exercises explained clearly makes it possible for us to follow them. “Free and useful” is to me, better than “Free and opaque”

Profile image of 455nefer
Submitted by 455nefer
about 11 years

Indeed. This is the third time I have had to look something up HERE. These guys ought to be helping us work through the problems. Step. By. Step.

Profile image of AriThereYet
Submitted by AriThereYet
about 11 years

My biggest pet peeve is when the hints say EXACTLY the same thing as the example text, that irks me because clearly I need the hint because I didn’t comprehend the above text. Other than that, I just go with the flow and try to see if I can catch on.

Profile image of anonymous
Submitted by anonymous
about 11 years

I feel like having to look around to fix my code is helping me remember the missing pieces better! I love Codecademy. These lessons have convinced me that I want to go back to school for computer science.

Profile image of aribilow
Submitted by aribilow
about 11 years

I personally find this and the last set of tutorials to be utterly mystifying. Even when, through hours of trial and error, I manage to pass an exercise, I do so having gained little or no comprehension of the intended concepts and it is only through trial and error that I gain some tertiary new knowledge of how python works.

I’m up to exercise 6/9 I have almost no understanding of defining functions, their limitations and structural specifics (i.e. sometimes I can print values out of them and sometimes I can’t and I have no clue why) and even less of an understanding of the interrelationship between them and lists/dictionaries let alone the actual ability to call specific values from them and extract useful information modularly… in other words, following a list of specific yet vague instructions != understanding.

Profile image of papasmf
Submitted by papasmf
almost 11 years

I will help you Ari! I got stuck on this too! Yer pal -JF

Profile image of papasmf
Submitted by papasmf
almost 11 years

haha

Profile image of papasmf
Submitted by papasmf
almost 11 years

crazy that i saw yer comment!

Answer 5172e1cc96a5264855002093

34 votes

Permalink

yes it did…!

What confuse me in this exercice is the fact that student dictionaries are at the beginning of the code so we just assume that the average function must be applied to students. It is wrong indeed but maybe change a little bit the instructions to make things more clear or remove the dictionaries from the first exercice are they are not relevant to the requirement ?

Profile image of guilsdenice
Submitted by guilsdenice
about 13 years

5 comments

Profile image of Slym
Submitted by Slym
about 13 years

Yeah, definitely! The instructions should be rewritten!

Profile image of smcnear
Submitted by smcnear
over 12 years

This entire section is SO much easier to follow than the last one. For me, the problem is STILL figuring out the right way to work with dictionaries and lists. I was never clear on when and how to use the key and when and how to use the value. If people are trying to use counting in these exercises, it’s probably because counting was never clearly explained.

As for me, this is the first section where I was able to “think like a programmer.” I understand that, if I want to use averages in my code, unless this is built into Python (like min(), max(), and abs()) I will ALWAYS have to start by writing the function myself. Maybe that’s what Michael means when he writes “faulty logic”.

Profile image of TomH83
Submitted by TomH83
over 12 years

I agree, the previous exercise was awfully difficult to follow. It seemed to make a HUGE leap in presuming our understanding of new concepts and instantly expected us to not only know how to use the new concepts but to combine them too. It has really slowed down my progress.

Profile image of coreJumper71318
Submitted by coreJumper71318
about 12 years

I have been stuck on these past two lessons for like two weeks! I am on the verge of giving up but I want to use python possibly to control an arduino. These lessons help me understand preexisting code which allows me to roughly modify for my spare components which I have. For me personally, this is just such a new concept that there is going to be a learning curve. The instructions should be a little clearer and more user friendly but on the same token nothing easy in this world is worth while…

Profile image of anonymous
Submitted by anonymous
over 10 years

I completely agree!! Sometimes it’s not clear what we have to do

Answer 52b0dcf680ff331c21000165

20 votes

Permalink

 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]
}
#
def average(lst):
    return float(sum(lst))/len(lst)
#
def get_average(student):
    # homework is 10%,quizzes are 30%,tests are 60%
    wtAvg=0
    wtAvg=average(student["homework"])*0.1\
    +average(student["quizzes"])*0.3\
    +average(student["tests"])*0.6
    return wtAvg
# get weighted avg
print str(lloyd["name"])+" has avg. of: "\
+str(get_average(lloyd))
print str(alice["name"])+" has avg. of: "\
+str(get_average(alice))
print str(tyler["name"])+" has avg. of: "\
+str(get_average(tyler))
Profile image of billjacktuna
Submitted by billjacktuna
over 12 years

5 comments

Profile image of byteAce19641
Submitted by byteAce19641
about 12 years

It doesn’t work!

Profile image of billjacktuna
Submitted by billjacktuna
about 12 years

i can assure you it does. suggestion … get Sublime Text 3, or 2 > create a python project … add code to project, as is above, build and voila. if running on a mac you may or may not have to put the shabang or #!usr/bin/python … and if on windows you need to get an interpreter installed. good luck … else use the browser interpreter :)

Profile image of Brownbaseball
Submitted by Brownbaseball
about 11 years

THANK YOU!!!!!!!! I tried like 50 different ways and couldn’t get it!

Profile image of devSolver75006
Submitted by devSolver75006
over 10 years

Thanks for the lead Goran, your work helped me a great deal. I found the specification odd and difficult to understand.

Profile image of devSolver75006
Submitted by devSolver75006
over 10 years

I was attempting do this with a for or while loop indexing the list of students because I had been taught in ancient times that doing the calculations in the output section of the program was a bad idea for some reason.

Answer 533d9219282ae3a0b100016c

14 votes

Permalink

Finally cracked this one:

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]
}


classroom = [lloyd, alice, tyler]


#
def average(x):
    return float(sum(x))/len(x)


#
def get_average(student):
    result = 0
    result = average(student['homework']) * 0.1\
    + average(student['quizzes']) * 0.3\
    + average(student['tests']) * 0.6
    return result
    

#
def get_class_average(avgs):
    totalAvg = []
    for x in avgs:
        studentAvg = get_average(x)
        totalAvg.append(studentAvg)
    return average(totalAvg)

get_class_average(classroom)
Profile image of baldchamp1
Submitted by baldchamp1
about 12 years

13 comments

Profile image of betaRunner24958
Submitted by betaRunner24958
about 12 years

thanks it works

Profile image of arramsri
Submitted by arramsri
over 11 years

thanks….it helped me

Profile image of anonymous
Submitted by anonymous
over 11 years

Thanks. This helped me. :)

Profile image of opap
Submitted by opap
over 11 years

two days, same error. Tried everything. Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected

Profile image of virago750cc91
Submitted by virago750cc91
about 11 years

opap, I have the answer to you error, you get that error when the “ return average(results)” is indented too far, it should be indented one less than the “results.append(whatever_you_named_it)”

Profile image of devBlaster47925
Submitted by devBlaster47925
about 11 years

thx you )

Profile image of RibhavThakur
Submitted by RibhavThakur
about 11 years

Says Return out of function on line 49 but there is no line 49

Profile image of tagSolver29660
Submitted by tagSolver29660
almost 11 years

Thanks, I couldn’t figure out why it didn’t work, then… indentation… doh!

Profile image of dataMaster15861
Submitted by dataMaster15861
almost 11 years

thanks mate..

Profile image of GangstaCoder
Submitted by GangstaCoder
almost 11 years

That’s clean code there

Profile image of bobblast
Submitted by bobblast
almost 11 years

I love you comenters! Crono I’m with you. Stupid indentations!

Profile image of fikri.firman
Submitted by fikri.firman
over 10 years

thanks for this, i have been struggling with 8/9 part for several days. i guess the instructions need to be revised.

Profile image of devSolver75006
Submitted by devSolver75006
over 10 years

That is very efficient code indeed! Nice work.

Answer 5181e910c178ec9380002261

13 votes

Permalink

For my part, I wrote like this :-p

def average(l):
    return sum(l) / len(l)
Profile image of tagSurfer36151
Submitted by tagSurfer36151
about 13 years

12 comments

Profile image of betaJumper12785
Submitted by betaJumper12785
about 13 years

Thanks a lot for this one. My average function got me through that lesson, but then turned out to be broken in the Sending a Letter function.

Profile image of hoangdd
Submitted by hoangdd
almost 13 years

.look easy :))

Profile image of arrayCoder59549
Submitted by arrayCoder59549
almost 13 years

Nope, did not work :(

Profile image of marisbest2
Submitted by marisbest2
almost 13 years

As it shouldn’t. This will return an integer when the correct average should be a float

Profile image of mesqueeb
Submitted by mesqueeb
over 12 years

What is a float? I don’t recall learning this…

Profile image of DocOnCA
Submitted by DocOnCA
over 12 years

An “integer” is a whole number (i.e. 1), a “float” is a number with a decimal (i.e. 3.14). The subject was introduced back in 1.3 (Python Syntax > Data Types)

Profile image of chipAce23517
Submitted by chipAce23517
over 12 years

That’s not a good description of a float, since it could just as easily describe a decimal, which is distinct. A float is an imprecise decimal; it allows it to be rounded after a certain number of decimal places.

Profile image of systemPro50329
Submitted by systemPro50329
over 12 years

I would also like to point out that I do not recall float() specifically being introduced anywhere. Floats were indeed mentioned in lesson one, but specifically defining a float, even when going back and reviewing, I still don’t see it. Had to google it.

Profile image of TomH83
Submitted by TomH83
over 12 years

Can anyone help out with the different styles of brackets please? When do we use the different ones? I think I get it for creating variables, lists and dictionaries but recalling them back is a bit more difficult.

I was using [] in this average function as I wanted it to work through the whole dictionary but here I see I should be using ()

Thanks

Profile image of Cawarnold
Submitted by Cawarnold
over 12 years

[] is for lists, {} is for dictionaries.

Profile image of anonymous
Submitted by anonymous
over 12 years

i write the same… didn’t work lol

Profile image of bitAce24212
Submitted by bitAce24212
almost 12 years

Ok the answer above is correct. Although it took me some time to get the idea. Generally you really need to read the assignment. Which turns uot ti be a problem, this asks you to write a general function. And i was looking for a way to calculate the score of every student:

{def average(numbers): total=0 total=sum(numbers)+total total=float(total) total=total/len(numbers) return total}

Answer 5243332880ff331c52000541

6 votes

Permalink

def average(lst): return float(sum(lst))/len(lst)

This works.

Profile image of pyRunner90801
Submitted by pyRunner90801
over 12 years

5 comments

Profile image of digitalMaster04465
Submitted by digitalMaster04465
over 12 years

not for me :(

Profile image of thevoidboy
Submitted by thevoidboy
over 12 years

It did for me…don’t forget the TWO closing brackets before the /

Profile image of tatcoolx
Submitted by tatcoolx
over 12 years

huh, works

Profile image of courseNinja20736
Submitted by courseNinja20736
about 12 years

works fine just don’t forget to indent the return statement

Profile image of teddyrose
Submitted by teddyrose
almost 12 years

mine keeps on saying “get_average not defined”

Answer 51e08afd282ae39bb3000cc6

5 votes

Permalink

average([1,2])

won’t return 1.5,

average([1.0,2.0])

will return correctly on this code:

def average(x):
   return sum(x)/len(x)
Profile image of dangpzanco
Submitted by dangpzanco
almost 13 years

5 comments

Profile image of djanvk
Submitted by djanvk
almost 13 years

I get the same thing here average([1,2[) only returns 1.

Profile image of marisbest2
Submitted by marisbest2
over 12 years

It’s because of integer division. The instructions should now better explain that…

Profile image of smcnear
Submitted by smcnear
over 12 years

I’m a little confused about this. When writing the function that returns students’ weighted averages, can I use .1 or .3, or does it have to be 0.1 or 0.3? I noticed that some people used 0.10 or 0.30. Is this a matter of style, or is there a preferred notation?

Profile image of Cancelor
Submitted by Cancelor
over 12 years

So the correct code is…

def average(x): return float(sum(x))/len(x)

Profile image of jberry777
Submitted by jberry777
over 12 years

This helped a lot. I had mistakenly had everything in one set of parenthesis next to the float.

Answer 51cd61698c1ccc23e301552f

2 votes

Permalink

The instructions state:

“First, write a function average that returns the average value of a list filled only with numbers”

Should the function check whether the list which has been passed only contains numbers and If so, using only the knowledge we have gained from previous sections and still using sum, how do we achieve this?

Profile image of philuk2000
Submitted by philuk2000
almost 13 years

4 comments

Profile image of marisbest2
Submitted by marisbest2
almost 13 years

You can assume the list is all numbers.

Profile image of ArcBruno
Submitted by ArcBruno
almost 13 years

i thought exactly like philuk2000, i wrote a function that was checking if the list was composed only by numbers, just to realize it wasn’t needed xD

Profile image of smcnear
Submitted by smcnear
over 12 years

Good point, philuk. Now that’s sound logic.

Profile image of irsiya247
Submitted by irsiya247
over 12 years

I think the problem is the lesson is leading somewhere but does not give us the overview FIRST. If we knew the end result, we would probably realize the EXACT purpose of the function.

Answer 52003fb8f10c6006eb000c2a

2 votes

Permalink

Well, I was confused until now. Thanks all for posting. Very enlightening.

Patty

Profile image of Pattybailhao
Submitted by Pattybailhao
almost 13 years

Answer 52746a62abf821ddbd002c35

2 votes

Permalink

can they just make a button that show us the answer?

Profile image of jconte
Submitted by jconte
over 12 years

1 comments

Profile image of irsiya247
Submitted by irsiya247
over 12 years

EXACTLY!!! But then again forum participation would come to a halt. Maybe this is intentional.

Answer 528d220580ff33384c001dff

2 votes

Permalink

this is the shortest code that will work that people often overlook

def average(lst):
return float(sum(lst))/len(lst)
Profile image of DNeal4816
Submitted by DNeal4816
over 12 years

4 comments

Profile image of methodRunner12968
Submitted by methodRunner12968
over 12 years

Yes, you will not end up with syntax errors and get the “way to go!” at the bottom of the screen.

Profile image of Glynys
Submitted by Glynys
over 12 years

This was the one that finally worked for me. def average(lst): return float(sum(lst)) / len(lst) I was making it more complex than it called for, trying to average each student’s homework, tests, etc. The exercise is looking for a generic list averaging function.

Profile image of ajaxBlaster18236
Submitted by ajaxBlaster18236
about 12 years

This will get the green light but it doesn’t actually work in a script.

Profile image of codecore
Submitted by codecore
about 12 years

it worked but am curious about @Joe Reitman comment that it doesn’t actually work in a script?can it work?

Answer 52b0a15552f863a92c0032a0

2 votes

Permalink

###this works after i was almost giving up n I hope I wil save someone tym

def average(lst) : avr = float(sum(lst))/len(lst) return avr

students = [lloyd, alice, tyler]

for avg in students: print avg[‘name’] print average(avg[‘homework’]) print average(avg[‘quizzes’]) print average(avg[‘tests’])

Profile image of fremax
Submitted by fremax
over 12 years

5 comments

Profile image of Little_CodeLikeJesus
over 12 years

how old are you fridah ?

Profile image of Godblessmycode
Submitted by Godblessmycode
over 12 years

Recruiting old sport?

Profile image of fremax
Submitted by fremax
over 12 years

does age matter really?

Profile image of Godblessmycode
Submitted by Godblessmycode
over 12 years

Yes, yes it does old sport .

Profile image of fremax
Submitted by fremax
over 12 years

am in my early 20’s n btw wat all abt old sport??

Answer 5302adb6548c35d5ed0011f6

2 votes

Permalink

k, a question regarding this matter: why can’t we just import the average.math function?…or something like that. maybe it’s a stupid question but i must know. thanks

Profile image of s7c
Submitted by s7c
about 12 years

1 comments

Profile image of 455nefer
Submitted by 455nefer
about 11 years

It’d math.average, and I’m not entirely certain that is a function.

Answer 521fa83d80ff3361150000bc

1 vote

Permalink

Well, I wrote the code and ended with the right answer straight away. So maybe is after all logic, or the way one see the problem and another see it another way. But I think it`s normal to be like this.

Profile image of designPro17059
Submitted by designPro17059
over 12 years

Answer 5233817880ff33e40e0017d6

1 vote

Permalink

def average(float[x]): return sum(x) / len(x)

This allowed me to pass for some reason, but I got a syntax error. Why is that?

Profile image of DAHnsly
Submitted by DAHnsly
over 12 years

9 comments

Profile image of anonymous
Submitted by anonymous
over 12 years

same for me man…. Do you know why????

Profile image of marisbest2
Submitted by marisbest2
over 12 years

When I run that code it doesn’t pass…

Profile image of DAHnsly
Submitted by DAHnsly
over 12 years

Houston, we have a bug.

Profile image of DAHnsly
Submitted by DAHnsly
over 12 years

def average(x): return float(sum(x)) / (len(x))

Profile image of marisbest2
Submitted by marisbest2
over 12 years

I’m confused… what are you submitting and what message are you getting?

Profile image of DAHnsly
Submitted by DAHnsly
over 12 years

My first post, I passed with, but received a syntax error. I know it’s not right but I’m so new to all this that I just try something until it works without knowing the rhyme or reason. I just wanted to make sure the function returned a float. So, the second average function worked for me. I really don’t know why. I wish there was a little more of an explanation of why this stuff works. I’ve been looking all over the web and found some stuff pertaining to that, but I think it would be cool if your site contained more of it. I think understanding the mechanics of why and how the code works might demystify it, but I know that’s not the site’s responsibility. Anyway . . .

Profile image of bartekrek
Submitted by bartekrek
over 12 years

Hi. I’m a noob myself but I’ll try to explain. Function float() changes it’s parameter value to float thus you can use it only on integer or string type variables and you were trying to use it on the list cause x you’re passing to your average() function is a list of numbers.

Your second average function is wrong but works because the list in the exercise you’re passing to it comprises of floats so float() function does nothing in this particular case. If it were integers it wouldn’t work. int/int = int (wrong value for example 10/4=2), float/int=float (correct) int/float=float (correct)

Profile image of DAHnsly
Submitted by DAHnsly
over 12 years

I’m confused. Why would it let me pass? What’s the correct answer?

Profile image of codePlayer10493
Submitted by codePlayer10493
about 12 years

I’m not sure

Answer 53795fe952f863cd46004f46

1 vote

Permalink

sorry for the indentation, forgot to enclose the tag

Profile image of anonymous
Submitted by anonymous
almost 12 years

Answer 53ac6e7252f86384df0009bd

1 vote

Permalink

I did it this way and it works def average(numbers): total = 0 if len(numbers) > 0: total = float(sum(numbers)) / len(numbers) return total else: print “No grades to average”

Profile image of j_standleyatt.net
Submitted by j_standleyatt.net
almost 12 years

Answer 5446c7287c82cac0f50006ec

1 vote

Permalink

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

def get_average(student): for student in students: total = [] homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”]) total = float(homework) * 0.1 + float(quizzes) * 0.3 + float(tests) * 0.6 return float(total) print total

ERROR Oops, try again. get_average(alice) returned 80.55 instead of the expected 91.15

Profile image of scriptSolver40740
Submitted by scriptSolver40740
over 11 years

2 comments

Profile image of teraPro10552
Submitted by teraPro10552
about 11 years

Oops, try again. get_average(alice) returned <function get_average at 0x7f56525ea0c8> instead of the expected 91.15

Profile image of ajaxWhiz32140
Submitted by ajaxWhiz32140
almost 11 years

i have the exact same problems :(

Answer 54bdd96c9113cb7a7f000e96

1 vote

Permalink

these instructions were the most frustrating part of this section - i get trying to challenge us but some of this was almost down-right misleading. Here are my interpretations of the instructions and below is my final (successful - hooray!) code in its entirety. I added some notes here and there, I hope they help. I bolded object names for the sake of clarity. I did change the name of the initial list, the word “student(s)” was getting thrown around too much.

The images don’t appear to be working too well (copying the code wasn’t cooperating, so I just took screenshots) but the image URLs should take you to the PNGs. The first is just the student dictionaries. The second image is the meat of the code but I wanted to provide everything for comparison

**Edit bajilliion.0 - in 9/9 the program is specifically looking for the “students” list to see if you’ve done it properly. my code in the screenshots still works fine but change “roster” to “students” in lines 44, 47 and 49. my solution to 9/9 looks like this:
(line 54) print get(underscore)class(underscore)average(students) (line 55) print get(underscore)letter(underscore)grade(get(underscore)class(underscore)average(students))

clear as mud?**

INSTRUCTIONS

  1. create a list, named roster, of your student dictionaries (the ones created at the beginning of your displayed code)
  2. define a function called get(underscore)class(underscore)average that accepts roster as the argument (the list you just created).
  3. within this function, prepare an empty list named results
  4. for each student in your roster, append your results list with the result of calling your get_average function with student as your argument.
  5. outside of the “for” loop but still within your function, return the class average by using the standard average function with your results list as the argument

alt text alt text

Profile image of AshAm2010
Submitted by AshAm2010
over 11 years

1 comments

Profile image of 455nefer
Submitted by 455nefer
about 11 years

Learn to add alt text. Just use text if images break and/or to be read by a screen reader

Answer 555f0aa676b8fe218300003f

1 vote

Permalink

This one is new You are all uncodeful

Profile image of anonymous
Submitted by anonymous
almost 11 years

Answer 555f0b1276b8fe7a21000294

1 vote

Permalink

You are all uncodeful

Profile image of anonymous
Submitted by anonymous
almost 11 years

2 comments

Profile image of Wispring
Submitted by Wispring
over 10 years

LOL!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Profile image of Wispring
Submitted by Wispring
over 10 years

you made reall laugh. Thanks ever so much!

Answer 55f45e1186f55221d90004b4

1 vote

Permalink

Super simple, just think about the basic math involved and what you’ve learned so far. There’s nothing here that hasn’t been covered. Here’s one example working solution:

def average(lst):
    return float(sum(lst)) / len(lst)
    
def get_average(student):
    grades = ['homework', 'quizzes', 'tests']
    
    weights = { 'homework': .1, 'quizzes': .3, 'tests': .6 }
    
    weighted_average = 0
    
    for key in student:
        if key in grades:
            weighted_average += float(average(student[key]) * weights[key])
            
    return weighted_average

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"
        
print get_letter_grade(get_average(lloyd))

def get_class_average(students):
    student_averages = [get_average(student) for student in students]
    return average(student_averages)
Profile image of BrooklynCarlo
Submitted by BrooklynCarlo
over 10 years

2 comments

Profile image of BrooklynCarlo
Submitted by BrooklynCarlo
over 10 years

Remember, no one said you can’t create an additional data set to help you solve the problem!

Profile image of Wispring
Submitted by Wispring
over 10 years

wowza. didn’t realize one could use a for next loop like you have just shown/shared when programming using the Python programming language. Reminds of of the BASIC that was imbedded in a Commodore64 i learned 28 years ago and over the years have subsequently forgotton alot of due to simply not using that particular version of BASIC. Reminds me of the saying “ The more things change, the more they stay the same if you can mentally process the essense of what I am communicating to you via this text entry. My first thought upon reading the intructors instructions was “How convoluted can you get?. Here i will clarify: his instructions: Define a function called get_class_average that has one argument…”students+. You can expect students to be a list containing your three students.{all right sir, it’s not i CAN expect. It’s I SHOULD and WILL expect because the contents of this singular lesson only has a list that only contains a listing of 3 students. First, make an empty list called results….OK….actually, What he wrote firstly is the instruction to define a function..right? Then the instructor of this course writes. “First, make and empty bla bla. Well partner… that is actually second in your psuedo-list of instructive material.Linear thought processing and then expressing such linear thought via (in this case English) should be..well…linear. You sir,(not you Carlo) are a source of confusion and I will in the future be using some high-level (haha) common sense when reading you “instructive reading material”

Answer 51e1ae2a9c4e9d4afd005a93

0 votes

Permalink

So is the following code fine?

def average(person):
    return sum(person) / len(person)
    
students = [lloyd, alice, tyler]

for avg in students:
    print avg['name']
    print average(avg['homework'])
    print average(avg['quizzes'])
    print average(avg['tests'])
Profile image of thatonedsm
Submitted by thatonedsm
almost 13 years

6 comments

Profile image of netSlayer57870
Submitted by netSlayer57870
almost 13 years

i did almost the same, but i think its not right

Profile image of satyam.shukla
Submitted by satyam.shukla
over 12 years

PLz try dis

avg = 0 def average(list): avg = sum(list)/((len(list)) * 1.0) return avg

Profile image of kp_569
Submitted by kp_569
over 12 years

This does not work.

Profile image of omarkhan
Submitted by omarkhan
over 12 years

Thanks, it worked!

Profile image of omarkhan
Submitted by omarkhan
over 12 years

Hi,kp_569 you just have to arrange it in the correct order.

Profile image of 455nefer
Submitted by 455nefer
about 11 years

how do you take the average of a name?!

Answer 524308ecabf821b9e5003889

0 votes

Permalink

It took me a while but i finally got it .. Issues i faced:

  1. if you do not have return in ur get average function it gives you an error saying “Oops, try again! Your code looks a bit off–it threw a “a float is required” error. Check the Hint if you need help!” which is not very clear as to what the real issue is

  2. I got another error saying i was trying to concatenate int to string when i tried to use a for loop with the dictionary object in get average function..so i just passed the static values for students list like student[‘homework’]

    def average(lst): num=len(lst) total=0 for item in lst: total=total+item return float(total)/num

def get_average(student): total=0.0 total=average(student[‘homework’])*0.1+average(student[‘quizzes’])*0.3+average(student[‘tests’])*0.6 return total

Profile image of webWhiz31812
Submitted by webWhiz31812
over 12 years

3 comments

Profile image of DAHnsly
Submitted by DAHnsly
over 12 years

Thank you much.

Profile image of PatriciaCCR
Submitted by PatriciaCCR
over 12 years

thnks :)

Profile image of PatriciaCCR
Submitted by PatriciaCCR
over 12 years

your get_average doesn’t work :(

Answer 524e5232f10c60b2e600234c

0 votes

Permalink

I know what the teacher is expecting now. The exercise is looking at the use of

def average(*args)

args will allow even one argument or as many arguments that the function call is using.

Try it out. It worked for me.

Profile image of xandersolis
Submitted by xandersolis
over 12 years

Answer 5267a85b548c35dd8800445c

0 votes

Permalink

Why this code does not work?

def average(lst):
    som = sum(lst)
    lon = len(lst)
    return float(som) / len(lon)
Profile image of Andy_Kwok
Submitted by Andy_Kwok
over 12 years

1 comments

Profile image of codePro15753
Submitted by codePro15753
over 12 years

You don’t need to use “len” twice. You use it to define lon, then you use “len(lon)” in your return. You already defined “lon” as len(lst).

Answer 527858c9548c35fb220093d7

0 votes

Permalink

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]
}


def average(lst):
    total = sum(lst)
    divide = len(lst)
    average = float(total)/divide
    print float(average)
    
average([100,92,98,100.0])

The Answer !

Profile image of Little_CodeLikeJesus
over 12 years

Answer 52a61c7d80ff3390ec00030b

0 votes

Permalink

Here is my solution after a hard time understanding the instructions :

def average(lst) :
avr = float(sum(lst))/len(lst)
return avr

print average([0]) print average([0,2]) print average([0,1])

Profile image of almesfer
Submitted by almesfer
over 12 years

1 comments

Profile image of scriptPlayer71442
Submitted by scriptPlayer71442
over 12 years

wow, finally got it to work. thanks. i’ve been struggling a bit w/ this one

Answer 52bb6a8a8c1ccc77b2005565

0 votes

Permalink

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] }

def average(lst): val = 0.0 for a in lst: val += a val /= len(lst) return float(val)

def get_average(dct): return (average(dct[‘homework’]) * 0.10) + (average(dct[‘quizzes’]) * 0.30) + (average(dct[‘tests’]) * 0.60)

#print ‘tyler average: %s’ % get_average(tyler) #print ‘alice average: %s’ % get_average(alice) #print ‘lloyd average: %s’ % get_average(lloyd)

Profile image of circakalos
Submitted by circakalos
over 12 years

Answer 52c11ffb52f863e326000f22

0 votes

Permalink

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] }

def average(lst): length = len(lst) avg = 0 for value in lst: avg += lst[value]/ length return float(avg)

def get_average(student): wavg = 0 wavg = average(student[“homework”])* 0.1 + average(student[“quizzes”]) * 0.3 + average(student[“tests”])* 0.6

return wavg

“”” hii all, i was trying to complete this challenge… but i am having trouble getting through with this …… Above is the code that i have written…. its giving me an error..Oops, try again. Does your get_average function take exactly one parameter (a student)? Your code threw a “list indices must be integers, not float” error.

I cant figure out the problem..Can you please help me with this…

thanks

harman

Profile image of betaAce32674
Submitted by betaAce32674
over 12 years

3 comments

Profile image of betaAce32674
Submitted by betaAce32674
over 12 years

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] }

def average(lst): length = len(lst) avg = 0 avg += float(sum(lst))/ length return avg

def get_average(student): wavg = 0 wavg = average(student[“homework”])* 0.1 + average(student[“quizzes”]) * 0.3 + average(student[“tests”])* 0.6

return wavg

“”” hi guys… i solved this by editing the average(lst) function……. but i am still confused …..i need to review whole of the concept again..

Profile image of jackywolfson
Submitted by jackywolfson
over 12 years

def get_average(student): wavg = 0.0 wavg = average(student[“homework”])* 0.1 + average(student[“quizzes”]) * 0.3 + average(student[“tests”])* 0.6 return wavg

Profile image of jackywolfson
Submitted by jackywolfson
over 12 years

wavg = 0.0 ,which means the format of float is essential here. The code above had made me pass.

Answer 52dc3989631fe94b8a00656c

0 votes

Permalink

def average(lst): return float(sum(lst))/len(lst) def get_average(student): return (average(student[‘homework’]) * 1/10) + (average(student[‘quizzes’]) * 3/10) + (average(student[‘tests’]) * 6/10)

finally it works.

Profile image of netRockstar91360
Submitted by netRockstar91360
over 12 years

Answer 5304e84c282ae3e91d008882

0 votes

Permalink

thanks all. Finally I learned the code. I just put the parentheses and the code worked

*def get_average (student): <<<<return (average(student[“homework”])*0.1)+(average(student[“quizzes”])*0.3)+(average(student[“tests”])0.6)

Profile image of tmenegaz
Submitted by tmenegaz
about 12 years

Answer 5314be43631fe994960003c4

0 votes

Permalink

def average(numbers): total = sum(numbers) float(total) print total /len(numbers)

Profile image of Retinus
Submitted by Retinus
about 12 years

Answer 532d8cdf282ae3736c003608

0 votes

Permalink

this worked..

def average(numbers): total = 0 total = sum(numbers) total = float(total) / len(numbers) return total

Profile image of jointops
Submitted by jointops
about 12 years

Answer 532fe8da282ae37ccc00a26f

0 votes

Permalink

7/9 - 9/9 Am I doing this right? I’m not sure about the last line.

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"        
print get_letter_grade(get_average(lloyd))

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

students = [lloyd, alice, tyler]
print get_class_average(students)
print get_letter_grade(get_class_average(students))
Profile image of zerny
Submitted by zerny
about 12 years

2 comments

Profile image of m_a_herrington
Submitted by m_a_herrington
about 12 years

I think the last return line on the get_class_average function is indented one too many times as it stands…

Profile image of zerny
Submitted by zerny
about 12 years

Sorry for the typo, it was supposed to be indented once only. Could you verify the last print line? Much appreciated.

Answer 536634c2631fe9d14c00378e

0 votes

Permalink

def average(numbers): sum(numbers) total=sum(numbers) total=float(total) total=total/len(numbers) return total

this worked for me, it was the 1st try even :D

Profile image of betaRunner93948
Submitted by betaRunner93948
about 12 years

Answer 5368c976548c35222c000083

0 votes

Permalink

def average(numbers): total = sum(numbers) total = float(total) return total / len(numbers)

Profile image of abhisam8686
Submitted by abhisam8686
about 12 years

Answer 53795fb29c4e9d5638005205

0 votes

Permalink

This is the code that got me past this headache maker

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

def get_average(student): homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”]) homework = homework * 0.1 quizzes = quizzes * 0.3 tests = tests * 0.6 avg = homework + quizzes + tests return avg

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: result = get_average(student) results.append(result) return average(results)

Profile image of anonymous
Submitted by anonymous
almost 12 years

Answer 53ac7230282ae3ed65000a06

0 votes

Permalink

Just wondering, my code in part 7/9 seems to work (I get the “Way to Go! Next Lesson –>” message) but all I see printed in the output is “None”. Has anyone’s code been printing out the actual results? I want to be sure that my code actually worked.

Profile image of spicard123
Submitted by spicard123
almost 12 years

1 comments

Profile image of bittuphilip
Submitted by bittuphilip
over 11 years

Hi Shaita, Put print before the function like print get_letter_grade(get_average(lloyd))

Answer 53bd13d6631fe98c35001874

0 votes

Permalink

It’s an really easy exercise, look what I did:

def average(l): return float(reduce(lambda x, y: x + y,l)) / len(l)

Profile image of Barel
Submitted by Barel
almost 12 years

1 comments

Profile image of 455nefer
Submitted by 455nefer
about 11 years

well, only if you knew to use reduce(lambda x, y: such-and-such)

Answer 53da1bab8c1ccccac400185c

0 votes

Permalink

Hi all

I’ve been running the 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) return (total / (len(numbers)))

def get_average(student): homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”]) average = (0.1 * homework) + (0.3 * quizzes) + (0.6 * tests) return (0.1 * homework) + (0.3 * quizzes) + (0.6 * tests)

def get_letter_grade(score): if score >= 90: return “A” elif score >= 80 and score <= 89: return “B” elif score >= 70 and score <= 79: return “C” elif score >= 60 and score <= 69: return “D” elif score < 60: return “F”

    print get_letter_grade(get_average(lloyd))

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

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

And I’m getting the following error-

Oops, try again. get_class_average([alice]) resulted in an error: local variable ‘average’ referenced before assignment

I keep on going round in circles. Can somebody please help?!

Profile image of mullet2856
Submitted by mullet2856
almost 12 years

3 comments

Profile image of gigaPlayer42102
Submitted by gigaPlayer42102
almost 12 years

I think it might be in def get_average where you assigned the local variable average = (0.1 * homework) + (0.3 * quizzes) + (0.6 * tests)?

Profile image of gigaPlayer42102
Submitted by gigaPlayer42102
almost 12 years

is there a confliction of using a local variable with the same name as a function?

Profile image of bittuphilip
Submitted by bittuphilip
over 11 years

Hi Scott ,

Kindly remove average = (0.1 * homework) + (0.3 * quizzes) + (0.6 * tests) from your code and it will work fine . :)

Answer 53dec612548c35d8af0033f0

0 votes

Permalink

can someone please explain what the second DEF does in the code above, many thanks def get_average(students): homework = average(students[“homework”]) quizzes = average(students[“quizzes”]) tests = average(students[“tests”])

return 0.1 * average(students["homework"]) + 0.3 * average(students["quizzes"]) + 0.6 * average(students["tests"])
Profile image of anonymous
Submitted by anonymous
almost 12 years

Answer 5405d515282ae38ce60070c6

0 votes

Permalink

Hi, could someone please explain to me why students=[lloyd,alice,tyler] and not [“lloyd”,”alice”,”tyler”] Thanks, Femi

Profile image of femi_
Submitted by femi_
over 11 years

1 comments

Profile image of FayusMaximus
Submitted by FayusMaximus
over 11 years

Because they aren’t strings. They are dictionaries and therefore you don’t use quotation marks with them. You’re creating a list with three different dictionaries. (= I hope this helps you.

Answer 5411c26a9c4e9dc47b0028f2

0 votes

Permalink

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

def get_average(student):
    homework = average(student["homework"]) * 0.1 **#line 28**
    quizzes = average(student["quizzes"]) * 0.3
    tests = average(student["tests"]) * 0.6
    return homework + quizzes + tests 
    
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"
        
print get_letter_grade(get_average(lloyd))


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

students = [lloyd, alice, tyler]
print get_class_average(students)
print get_letter_grade(get_average(students)) **#line 56**

It works, but I get these errors:

Traceback (most recent call last):
  File "python", line 56, in <module>
  File "python", line 28, in get_average
TypeError: list indices must be integers, not str
Profile image of gigaAce07736
Submitted by gigaAce07736
over 11 years

1 comments

Profile image of duanyz
Submitted by duanyz
over 11 years

“print get_letter_grade(get_average(students))” shuould be “print get_letter_grade(get_average(lloyd))”

Answer 541344c37c82ca2f350005cf

0 votes

Permalink

The problem is human interpretation versus syntax (x). Its unnatural for humans,or any organism likely to accept difference. I guess, the first thing you need to accept is that you don’t before you can understand it.

Thanks for the posts though, it made me aware of a problem i refused to accept.

Profile image of webSurfer88213
Submitted by webSurfer88213
over 11 years

Answer 54845214e39efe2e77000c83

0 votes

Permalink

def average(numbers): total = 0.0 for x in numbers: total += x

avg = total/len(numbers)
return avg

this worked for me, tried to understand most of it here, but didnt.

If it helps after not understanding anything here, I just re-read the assignment.

think about it, you only want the average returned of any list.

Profile image of nicmakaveli
Submitted by nicmakaveli
over 11 years

Answer 5494fc4395e3786b74002ad2

0 votes

Permalink

Hi All,

You can try my code this is working fine for 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] }

##Functions def average(numbers): total= sum(numbers) #print total total = float(total) #print total total = total/len(numbers) #print total return total

def get_average(student): homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”])

#Since Homework is 10% so we convert it into 10/100= 0.1
return 0.1*homework+0.3*quizzes+0.6*tests

print get_average(lloyd)

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”

print get_letter_grade(get_average(lloyd))

Profile image of bittuphilip
Submitted by bittuphilip
over 11 years

Answer 54a10a2395e378b1cb013a97

0 votes

Permalink

my solution:

def get_class_average (students): results= [] for item in students: results.append(get_average(item)) return average(results)

Profile image of atalexand
Submitted by atalexand
over 11 years

Answer 54b0e8cdd3292f2236003179

0 votes

Permalink

This worked for 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 = sum(numbers)
    total = float(total)
    return total / len(numbers)

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    homework = homework * .10 
    quizzes = quizzes * .30
    tests = tests * .60
    dingle = homework + quizzes + tests
    return dingle 

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: 
        asshole = get_average(student)
        results.append(asshole)
    return average(results)

print get_class_average(students)
score = get_class_average(students)
print get_letter_grade(score)
Profile image of alegionofgeniusgmail.com
over 11 years

Answer 54b7e7029113cb9f330029e6

0 votes

Permalink

So what messed me up on this part of the course is that the concept of variable ‘scope’ and namespace for functions are not really mentioned in the tutorial up to now and if you are not careful and you use a variable name that is the same as a ‘function’ name - oh say you use the variable name ‘average’ and have a function called ‘average’ then it will mess things up completely. Make sure no variables have the same names as functions. That worked for me.

Profile image of betaRunner71414
Submitted by betaRunner71414
over 11 years

Answer 54e3de4295e378ba3c001429

0 votes

Permalink

This worked for me:

def average(student): return float(sum(student)) / len(student)

be sure to indent return line

Profile image of mondovila
Submitted by mondovila
about 11 years

1 comments

Profile image of jaxelrod
Submitted by jaxelrod
almost 11 years

I indent the return line and it gives me an ‘unexpected indent’ error!!!! Then when I unindent it gives me an ‘outside of function’ error.

Answer 5515d01fd3292fb45700007b

0 votes

Permalink

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] }

def average(numbers): total = float(sum(numbers)) return total/len(numbers) def get_average(student): homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”]) return homework*.1 + quizzes*.3 + tests*.6

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” print get_letter_grade(get_average(lloyd))

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

Profile image of courseSolver10643
Submitted by courseSolver10643
about 11 years

Answer 551ed9c695e378c1b500028b

0 votes

Permalink

This code worked for me:

def average(numbers): sum(numbers) total = sum(numbers) return float(total) / len(numbers)

Profile image of Achmed4_
Submitted by Achmed4_
about 11 years

Answer 553598499113cba412000048

0 votes

Permalink

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)

float(total)

result=total /len(numbers)

return result

def average(lst) : avr = float(sum(lst))/len(lst) return avr def get_average(student): homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”]) return 0.1 * homework + 0.3 * quizzes +
0.6 * tests students = [lloyd, alice, tyler] 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” a=get_average(lloyd) result=get_letter_grade(a) print result def get_class_average(students): results=[] for student in students: result=get_average(student) results.append(result) return average(results)

print get_letter_grade(get_class_average(students)) 

print get_class_average(students)

Profile image of objectRockstar29004
Submitted by objectRockstar29004
about 11 years

Answer 5559c8059113cb259c00054f

0 votes

Permalink

Why are there so many answers?!

##And none of them work! ##

Profile image of 0josh0
Submitted by 0josh0
almost 11 years

Answer 55670c6276b8fe3e34000210

0 votes

Permalink

This is how I wrote it:

def average(numbers):
    total = 0
    for x in numbers:
        total += x
    total = total/float(len(numbers))
    return total

Hope this helps.

Profile image of cloudPlayer90408
Submitted by cloudPlayer90408
almost 11 years

Answer 5567143ce39efe3700000978

0 votes

Permalink

You’re an ungrateful bunch. This section is hard but not impossible. Just re-read the tasks carefully and it DOES work. If I can manage it, then anyone can

Profile image of JebusKryst
Submitted by JebusKryst
almost 11 years

Answer 5569c2939113cbda58000214

0 votes

Permalink

Hello,

Here is my code. It works perfectly fine, you may have a look at it.

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”]) quizzes=average(student[“quizzes”]) tests=average(student[“tests”])

return (0.1*homework)+(0.3*quizzes)+(0.6*tests)

def get_letter_grade(score): if score>=90: return “A” elif score>=80 and score<90: return “B” elif score>=70 and score<80: return “C” elif score>=60 and score<70: return “D” else: return “F”

#Just for testing purpose
grade=get_letter_grade(get_average(lloyd)) print “Lloyd’s Grade is %s” %grade

def get_class_average(students): results=[] for item in students: studentAvg = get_average(item) results.append(studentAvg) return average(results)

students = [lloyd,alice,tyler] print get_class_average(students) print get_letter_grade(get_class_average(students))

Profile image of courseSurfer16878
Submitted by courseSurfer16878
almost 11 years

Answer 55767869937676e9dd00050d

0 votes

Permalink

I created an empty list oust side of the get_class_average function and couldn’t move on but when I erased that and created the empty list inside the function It worked perfectly. Just in case anyone is pulling their hair out with the same problem.

Profile image of anonymous
Submitted by anonymous
almost 11 years

Answer 557aa114e39efe66e500013b

0 votes

Permalink

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) average = float(total)/len(numbers) return average def get_average(student): homework = student[“homework”] quizzes = student[“quizzes”] tests = student[“tests”] return 0.1average(homework)+0.3average(quizzes)+0.6*average(tests) def get_letter_grade(score): if score>=90 : return “A” elif 90>score>=80 : return “B” elif 80>score>=70 : return “C” elif 70>score>=60 : return “D” else : return “F” x = get_average(lloyd) y = get_letter_grade(x) print y def get_class_average(students): results = [] for student in students: result = get_average(student) results.append(result) return average(results) students = [lloyd,alice,tyler] print get_class_average(students) print get_letter_grade(get_class_average(students))

Profile image of anonymous
Submitted by anonymous
almost 11 years

Answer 559384bdd3292f646f000863

0 votes

Permalink

Hi All,

First time contributing! I noticed a lot of complex answers given here and confusion over what was asked, so I just wanted to post my code and explain the logic in the hope it might help someone else:

#per 01, I define the function average(numbers) def average(numbers): #per 02&03, I create var total using sum() and float() total = float(sum(numbers)) #per 04 I create a new var result for average of total result = total / len(numbers) #per 05 I return that result return result

Profile image of ojconnolly
Submitted by ojconnolly
almost 11 years

Answer 559e7b3ed3292ff3f00003f1

0 votes

Permalink

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

def get_average(student): homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”]) return homework * 0.1 + quizzes * 0.3 + tests * 0.6

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”

print get_letter_grade(get_average(lloyd))

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

students = [lloyd, alice, tyler]

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

#That should be the correct question #Plz Notice that the indent may cause some problem

Profile image of webNinja23825
Submitted by webNinja23825
almost 11 years

Answer 55b64c9d76b8fe61f0000400

0 votes

Permalink

that’s mine:


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) return total / len(numbers) def get_average(student): homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”]) return (homework * 0.1) + (quizzes * 0.3) + (tests * 0.6)

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” print get_letter_grade(get_average(lloyd))

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


Profile image of teraAce74611
Submitted by teraAce74611
almost 11 years

Answer 55c8a7a6e39efe140300022f

0 votes

Permalink

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

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

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"

print get_letter_grade(get_average(lloyd))


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

students = [lloyd, alice, tyler]
print get_class_average(students)
print get_letter_grade(get_average(lloyd))
Profile image of kryptiq
Submitted by kryptiq
almost 11 years

Answer 5602f0aa3e0ec859310000bb

0 votes

Permalink

I will always remember about indentations! I will always remember about indentations! I will always remember about indentations! I will always remember about indentations! I will always remember about indentations!

Profile image of anonymous
Submitted by anonymous
over 10 years

Answer 5614f9169113cb146400015b

0 votes

Permalink

for student in students: print student[“name”] print student[“homework”] print student[“quizzes”] print student[“tests”]

def average(numbers): total = sum(numbers) total = float(total) return total / len(numbers)

def get_average(student): homework = average(student[“homework”]) quizzes = average(student[“quizzes”]) tests = average(student[“tests”]) return 0.1 * homework + 0.3 * quizzes + 0.6 * tests

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”

print get_letter_grade(get_average(lloyd))

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

Profile image of courseWhiz97082
Submitted by courseWhiz97082
over 10 years

Popular free courses

  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      4 Lessons
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      11 Lessons
  • Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
    • Beginner Friendly.
      6 Lessons
Explore full catalog