This forum is now read-only. Please use our new forums at discuss.codecademy.com.

1703 points
4f84976a93d918000300de59_981251210
Submitted by
Michael Rochlin
about 6 years ago

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!


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.

312 points
98c6b0020668edf140529e9ce8dc6c30?s=140&d=retro
Submitted by
Taylor Belcher
about 6 years ago

45 Comments

949f3fa093aad55a7e1767b78e90b00a?s=140&d=retro stgeorge about 6 years ago

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.

4f84976a93d918000300de59_981251210 Michael Rochlin about 6 years ago

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

949f3fa093aad55a7e1767b78e90b00a?s=140&d=retro stgeorge about 6 years ago

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?

949f3fa093aad55a7e1767b78e90b00a?s=140&d=retro stgeorge about 6 years ago

From: Just average 04/05:

"Write a function called getclassaverage 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.

4f84976a93d918000300de59_981251210 Michael Rochlin about 6 years ago

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

949f3fa093aad55a7e1767b78e90b00a?s=140&d=retro stgeorge about 6 years ago

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

5183fdb943d25a82a1000b84_155516738 Brad almost 6 years ago

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 getclassaverage function.

51c8fe367c82ca174b006e2e_66176561 e4Nf3Bb5 almost 6 years ago

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.

51f26e8452f8636c86002fda_647082642 word4word constantine almost 6 years ago

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.

873993f8ecd35d4ca600cedcc7ba3338?s=140&d=retro andy gray almost 6 years ago

I agree with the comments above

F3f7c7615aa8973b9f317ee9e4af0701?s=140&d=retro elizan almost 6 years ago

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.

Ab5b631b00921b8e91338419aa9df726?s=140&d=retro MC almost 6 years ago

I also agree

51fd393080ff338c700035bc_323749321 Diego J.B. over 5 years ago

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)

E82cc94bc35887124eec58902e0e0b8f?s=140&d=retro William Lee over 5 years ago

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?

E82cc94bc35887124eec58902e0e0b8f?s=140&d=retro William Lee over 5 years ago

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

Picture Chris Rocco over 5 years ago

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

54cd543e8c2a806a6e7a4fd705957ba8?s=140&d=retro Jared Stensland over 5 years ago

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

8c887d03a5d55260b65527b79f88d573?s=140&d=retro CryptoCoinSolutions over 5 years ago

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.

Cd18d1816021c8b05365934ec122a829?s=140&d=retro michael about 5 years ago

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.

6b4558d92cac6c42737412d6c9669560?s=140&d=retro agrabloj almost 5 years ago

my thoughts exactly

7b6a4f5fd7d4eb0001227c35db6be7e2?s=140&d=retro Dan McFarland almost 5 years ago

I also agree.

Picture Joseph Denton almost 5 years ago

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.

Picture harry93rose almost 5 years ago

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

36baf02b872883b701b93fccd9bfa6e7?s=140&d=retro pancakerz over 4 years ago

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.

11ba5d9127e3598fa88d735d544c4fe4?s=140&d=retro Matheen Ali over 4 years ago

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

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

A604cc4bc11e755c298a170a8f928da0?s=140&d=retro jufranzoi over 4 years ago

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 ...

826fce46735fa969218ec7cb3995a925?s=140&d=retro CptJHarkness over 4 years ago

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.

  1. steps 2, 3, and 4 can be placed here with some minor editing
  2. 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.

577074801026d9c0c2000391_784614504 Dat Mee over 4 years ago

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

54b76cc186f552d64b0019ac_868234177 Jeffrey George over 4 years ago

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.

54b3fb1b95e37888d0008584_256668733 Lucy Carrick over 4 years ago

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

51ea133f9c4e9d7dda001b65_106128941 Diego Villena over 4 years ago

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.

58049a9292f40731c700030f_409617585 90 over 4 years ago

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.

2e17870df63f227748386b7f01bd9c9a?s=140&d=retro TheNikodilay_54f391499113cb18ff00551 about 4 years ago

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

54b3fb1b95e37888d0008584_256668733 Lucy Carrick about 4 years ago

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.

Picture positiveprogrammer about 4 years ago

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..

58049a9292f40731c700030f_409617585 90 about 4 years ago

I'd actually consider myself guilty of that...

77e695be95fcdd06d2b3f28a8abf5b9f?s=140&d=retro 455nefer about 4 years ago

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?

Ef896f5c530f6111d71211c2ae882a5d?s=140&d=retro astarrh about 4 years ago

Some people have no respect

55675c82937676656c000693_978207861 Shane Stout almost 4 years ago

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.

5759dade4c432cab96000511_23458869 Sam almost 4 years ago

@Shane Stout exactly ^^^

Cdedbc9f59b4cb19f4bdb8620102bcdc?s=140&d=retro GangstaCoder almost 4 years ago

ditto

6604f799c7d0b849018db35704367691?s=140&d=retro Bex almost 4 years ago

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.

F6f96ce5c7053921db0175be8c133e69?s=140&d=retro Fredrik over 3 years ago

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.

04e09cf598e1f457e1a462247c4dfbc6?s=140&d=retro Brian Stillwell over 3 years ago

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.

7105908f50736baa5457acfa0a99cde9?s=140&d=retro BobGears over 3 years ago

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


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))

298 points
5268c66fabf82196140031e7_946768412
Submitted by
一只特立独行的猫
about 5 years ago

73 Comments

11c257690b9761b4bdd1915ceeb3afb0?s=140&d=retro Antonio HP about 5 years ago

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

537642f17c82ca37db00017a_53299987 Tae Keun Lee about 5 years ago

Thank you very much :)

552fa41d86f55226e7000061_720625731 Geoffrey Ayap almost 5 years ago

thanks so much

53d9c34f80ff334f18000c39_426803975 Mutembei almost 5 years ago

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.

2f79a006f7cdf6f7211d6eb34f6cec4a?s=140&d=retro arsalanj123 almost 5 years ago

Thanks

5391381f9c4e9d39a20004c7_173915540 pmporter almost 5 years ago

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

print getclassaverage([alice, lloyd, tyler])
print getlettergrade(getclassaverage([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.

74e3b868a9c2b2f68ade14629d29ec01?s=140&d=retro doctor_boy almost 5 years ago

Thanks!

Aac9d7ea6a685c47fbcdbdfea40cd8ff?s=140&d=retro Akin Okon almost 5 years ago

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

Picture Sweta Shaw almost 5 years ago

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

E07f206dda262d72ac5497b6825156b0?s=140&d=retro yebuoy almost 5 years ago

it worked! thanks

1a848f28fbd8b8c8894af07c0914adfc?s=140&d=retro aj unknow almost 5 years ago

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.

Af068047e9fa9ab37d6768dd83547eee?s=140&d=retro Andrew L almost 5 years ago

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.

1a848f28fbd8b8c8894af07c0914adfc?s=140&d=retro aj unknow almost 5 years ago

cheers andrew m8 that is a much clear

02fc07c995f8fa791d4a1d436fc5c6fc?s=140&d=retro frank1701a almost 5 years ago

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.

Picture Nathaniel Musial almost 5 years ago

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?

534d4f07dd66a6de8cb8367ab06df38b?s=140&d=retro ceebs almost 5 years ago

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.

Picture clearwaters over 4 years ago

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

736115862b59bb77a9bee0a84589513f?s=140&d=retro Yalama over 4 years ago

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

3b410da5fbcdbfd06f38dff7d56cccca?s=140&d=retro Ryan Mohan over 4 years ago

works perfect, the part i was missing in the getclassaverage() 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

541e8645282ae378020038f9_4670774 Rashid Malik over 4 years ago

Yea, this works

42a5e9667e4e23a85cb11caedeb4da9d?s=140&d=retro Luis over 4 years ago

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

5a0727c172fc73d1fd06de597a415672?s=140&d=retro patrascual over 4 years ago

Thanks!

8bbe44cc717eaa9369880a46d8959c0a?s=140&d=retro femke11 over 4 years ago

thank you so much!!

88a7039869d163fb7d67759aafd2ef25?s=140&d=retro tradefutures over 4 years ago

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

545a3f83631fe94671004539_565637013 minhwf_545a3e809c4e9d9968003f54_dele over 4 years ago

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

51c42bfc631fe927c300f387_747788487 Kit Rothwell over 4 years ago

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

565af5fa95e3789779000486_538526949 Need For Speed over 4 years ago

you are literally a legend

565af5fa95e3789779000486_538526949 Need For Speed over 4 years ago

votes for u. :)

56ddee4ff50c68125300069b_238429509 Igor over 4 years ago

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

548f083451b887acf3008cef_701846344 LegoBatmanMaster over 4 years ago

YOU ARE... DA BEST.

548f083451b887acf3008cef_701846344 LegoBatmanMaster over 4 years ago

YYYYYYYAAAAAAASSSSSSS!!!!!!!

Picture Adam Rutkowski over 4 years ago

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

89b4746059af4979998265433f922c0a?s=140&d=retro manndaw11 over 4 years ago

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

Picture Cindy Conley Taylor over 4 years ago

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

Cba2922892cdf6327300aba18f7461f4?s=140&d=retro meetthecool shah over 4 years ago

thnak you
working..:)

2c7fb2efdfa786e8f5ac300c2f6e1e5d?s=140&d=retro Slava over 4 years ago

Oh, finally, thank you!

546a3fd67c82ca329d00040e_653614803 nav over 4 years ago

thanks!!!

7b2628c1969571727e0cc59d7bba3b9c?s=140&d=retro Siri Sadashiva over 4 years ago

thank you!!

7bd13b8ad0c9abd8fb4834687f085090?s=140&d=retro Adam over 4 years ago

thanks

51ea133f9c4e9d7dda001b65_106128941 Diego Villena over 4 years ago

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.

Picture Robb Rayfield over 4 years ago

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

B4e25c1ae3b7426e0f2cd34eaffd1d34?s=140&d=retro Ethan Colby-Witanek over 4 years ago

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.

F92c10215f1df4ceecb869085d5a9706?s=140&d=retro Daniel Olsen over 4 years ago

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.

0da84f0ebd12eb1acafc2b27ce5701c4?s=140&d=retro viresh92 over 4 years ago

thenx dear

185831571ecbb30de97206c3ffbeee15?s=140&d=retro ALE ALE ALE ALEJANDRO about 4 years ago

Thank you, I can go to bed now.

Dbd8b45340a5c8c3fcc5b4ee05b6135b?s=140&d=retro Asif Mohammad about 4 years ago

thank you...:)

Picture Nikolay Tanev about 4 years ago

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

Fabdd0f883488644c792e17cd66cb854?s=140&d=retro j_mazolenyyahoo.com about 4 years ago

Thank you

Cb839820822339c98a040832016f6499?s=140&d=retro Kane Bazuka about 4 years ago

thank you))
it works

64c789dbad14d25d0ae70dd49199f00a?s=140&d=retro Gareth Knights about 4 years ago

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

4183e1d84abb3eafb416adb3dc59382c?s=140&d=retro Brian Blanchard about 4 years ago

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)"

Picture Lata Peng about 4 years ago

Thanks.this solved my problem.

5528ec14937676e50300005a_128925136 Rishabh Sahrawat about 4 years ago

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

4a0329172c9cbb68eb3e7a671973e27c?s=140&d=retro AriThereYet about 4 years ago

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.

553781de86f552b58b0000c9_705844812 ElenaR Buhus about 4 years ago

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

3298353e6dca6d649a662427b8d76f8e?s=140&d=retro Richard Smith about 4 years ago

thakyou :) saved me ALOT of time

1a9e8c7f56d7db81e3a855a45013d5fc?s=140&d=retro орест муха about 4 years ago

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

773296bc4ae6e33243d366b1370aac9c?s=140&d=retro Abhinandanudupa about 4 years ago

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

08b1d13ee515c936598a5f9a6bcf9a5b?s=140&d=retro RowanStewart about 4 years ago

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

93a2084797fbeaac83825f39b4f5fca3?s=140&d=retro budf almost 4 years ago

Thank you!!!! Nice work.

4eae6f02dab4a65a350831ea3b2b72c5?s=140&d=retro smyles21 almost 4 years ago

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. getclassaverage([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?

Picture Pinzonc almost 4 years ago

why don't you do print average(getclassaverage(students))

Picture Jeremiah Stanley almost 4 years ago

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.

59cac46d92f40799cd000249_250893961 Aaron Castrejon almost 4 years ago

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

F757b2f336854de550180abe7fc99044?s=140&d=retro Monika almost 4 years ago

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!

54f27f01d3292f62db0037e1_192471251 Yumei Leventhal almost 4 years ago

Thanks! So glad to get past this idiotic lesson!

8f01d9cdd7d22a425c923cdc6c7fa9a6?s=140&d=retro aomcastor almost 4 years ago

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

55b5ad25d3292f5c4a000264_212771539 He almost 4 years ago

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.

657f67406895d46cd0ea71abe9b11c48?s=140&d=retro Fred444 almost 4 years ago

Yes, He, that is right, you just need 'return 0.1 * homework....'

Picture Vaish almost 4 years ago

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

5505ef75937676c400007031_728476922 Corey Gumbs over 3 years ago

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.

Dafabcc7fff906d51b7a9ce168e87eb6?s=140&d=retro Ihaveafork over 3 years ago

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

051c3f0c48342496eb58043421130ef8?s=140&d=retro esraaelkady over 3 years ago

thank you soooo much


51 votes

permalink

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

620 points
0d8648f850a16ebc22212b812681239a?s=140&d=retro
Submitted by
themoose00
over 5 years ago

11 Comments

52cf4aee548c355b6800072a_291139768 Paul Lukitsch over 5 years ago

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.

Picture JB820901 about 5 years ago

^^ this guy, I like this guy ^^

51805d14384114fd02000226_994014589 Jeff van Geete almost 5 years ago

^^seconded

3fea6ec7052841ac01a6b24e3523f657?s=140&d=retro lumogas over 4 years ago

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"

77e695be95fcdd06d2b3f28a8abf5b9f?s=140&d=retro 455nefer about 4 years ago

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.

4a0329172c9cbb68eb3e7a671973e27c?s=140&d=retro AriThereYet about 4 years ago

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.

1144481abf1656f490dcd06d713eb650?s=140&d=retro mathewmcdade_505a5ad60452970002003df about 4 years ago

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.

A40c0d2d238bab9fc69ce944b2eff044?s=140&d=retro aribilow about 4 years ago

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.

D1444c69da4d704d209df9e625e7dfdc?s=140&d=retro papasmf almost 4 years ago

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

D1444c69da4d704d209df9e625e7dfdc?s=140&d=retro papasmf almost 4 years ago

haha

D1444c69da4d704d209df9e625e7dfdc?s=140&d=retro papasmf almost 4 years ago

crazy that i saw yer comment!


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 ?

309 points
8dcaeb92a11f6a9a9eb53f03934e3e87?s=140&d=retro
Submitted by
Gilles
about 6 years ago

5 Comments

50bcee7e2a33b778bb001301_125457397 Slym about 6 years ago

Yeah, definitely! The instructions should be rewritten!

5221ba95f10c6032f600090c_781597413 SheriBoBerry over 5 years ago

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".

Af21601540d41e3e96367b7541c1e756?s=140&d=retro Tom Holdstock over 5 years ago

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.

Picture JB820901 about 5 years ago

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...

A24363e78b227305d9d6e20c9f217feb?s=140&d=retro Patricia Fonseca over 3 years ago

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


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))

547 points
Eb5192716645859eb76e90426028214d?s=140&d=retro
Submitted by
Goran B
over 5 years ago

5 Comments

Eeb38d6a215e1b7a4608470af8abdc32?s=140&d=retro yan long about 5 years ago

It doesn't work!

Eb5192716645859eb76e90426028214d?s=140&d=retro Goran B about 5 years ago

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 :)

4757150abecec15249b7b37607cd41ef?s=140&d=retro Brownbaseball about 4 years ago

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

55562a309113cb18fe000502_137210827 Almis Peter Salcius over 3 years ago

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

55562a309113cb18fe000502_137210827 Almis Peter Salcius over 3 years ago

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.


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)

408 points
538a552952f863c732000e6b_522586708
Submitted by
Imer Pacheco
about 5 years ago

13 Comments

52a0452b7c82cafbef001f29_965774386 Kunal Patel about 5 years ago

thanks it works

E5ed4d6b27850bbfc487341512f689ec?s=140&d=retro arramsri over 4 years ago

thanks....it helped me

5430c85d52f863bcb40047c4_440226694 Jen Hodges over 4 years ago

Thanks. This helped me. :)

E073a2c4266c8bcb9ffece3c599b657e?s=140&d=retro opap over 4 years ago

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

681de89bfcb2ceca440ed5e1923f959d?s=140&d=retro virago750cc91 about 4 years ago

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(whateveryounamed_it)"

Cb839820822339c98a040832016f6499?s=140&d=retro Kane Bazuka about 4 years ago

thx you )

5528e7329113cb5ac4000029_697934889 Ribhav Thakur about 4 years ago

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

A25e4c464f75d1593f1a47c37a5bd365?s=140&d=retro Crono Mante almost 4 years ago

Thanks, I couldn't figure out why it didn't work, then... indentation... doh!

Picture Niraj Hirachan almost 4 years ago

thanks mate..

Cdedbc9f59b4cb19f4bdb8620102bcdc?s=140&d=retro GangstaCoder almost 4 years ago

That's clean code there

Ce0bf73b6e8d98a0abe3147ea9c63f82?s=140&d=retro Ryan Ricketts almost 4 years ago

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

9c161f8169e3c8b0068d35c8c9c00490?s=140&d=retro fikri.firman over 3 years ago

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

55562a309113cb18fe000502_137210827 Almis Peter Salcius over 3 years ago

That is very efficient code indeed! Nice work.


13 votes

permalink

For my part, I wrote like this :-p

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

648 points
558e04279113cbaa540001ee_618511545
Submitted by
Huan Phan
about 6 years ago

12 Comments

Babf37d1f7ed33e9c99652994d609352?s=140&d=retro Nicholas Teeple about 6 years ago

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.

A14ea8aadec1a41b2d4dd585a2da7918?s=140&d=retro Hoàng Đd almost 6 years ago

.look easy :))

Picture Anna Kutyrina almost 6 years ago

Nope, did not work :(

4f84976a93d918000300de59_981251210 Michael Rochlin almost 6 years ago

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

29f1cd3f59b8074183043251068b693d?s=140&d=retro the Great almost 6 years ago

What is a float? I don't recall learning this...

4bd6cbd437dd9b69c355534d3d581559?s=140&d=retro DocOnCA over 5 years ago

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)

Picture Chris Clasby over 5 years ago

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.

Picture Justin Ardoin over 5 years ago

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.

Af21601540d41e3e96367b7541c1e756?s=140&d=retro Tom Holdstock over 5 years ago

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

03759ae73dbd2b352d7f49be54714d0a?s=140&d=retro Cawarnold over 5 years ago

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

524ed00d80ff33ebf700403c_247588447 Chetan over 5 years ago

i write the same... didn't work lol

Picture Silvijus Abrambaltazar almost 5 years ago

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}


6 votes

permalink

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

This works.

809 points
De18f4d906fb4d5dd3701666e21e11bb?s=140&d=retro
Submitted by
Oliver Gilbert
over 5 years ago

5 Comments

524c983ff10c60971d005a53_620944548 Jennifer Basalone over 5 years ago

not for me :(

Picture J David Hester over 5 years ago

It did for me...don't forget the TWO closing brackets before the /

50d9c63e1a1e56e75b004f33_472932985 Alexander Surmenidi over 5 years ago

huh, works

Picture Conor O'Brien over 5 years ago

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

4ad131beff62988ad5b2622874c8b439?s=140&d=retro teddyrose almost 5 years ago

mine keeps on saying "get_average not defined"


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)

355 points
9ce3de6c33b9f4620c57acd8b76ba390?s=140&d=retro
Submitted by
Daniel Zanco
almost 6 years ago

5 Comments

Picture Douglas Kohn almost 6 years ago

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

4f84976a93d918000300de59_981251210 Michael Rochlin almost 6 years ago

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

5221ba95f10c6032f600090c_781597413 SheriBoBerry over 5 years ago

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?

B517eb65702eeac8db3494159a74e8b7?s=140&d=retro Cancelor over 5 years ago

So the correct code is...

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

54e509ec76b8fe99ab003ba0_718173505 Enviy over 5 years ago

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


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?

350 points
Fe59ef0700c50884625bd9463fc6f318?s=140&d=retro
Submitted by
philuk2000
almost 6 years ago

4 Comments

4f84976a93d918000300de59_981251210 Michael Rochlin almost 6 years ago

You can assume the list is all numbers.

50bc504626b5a74207004ccb_126141110 Bruno Saraiva almost 6 years ago

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

5221ba95f10c6032f600090c_781597413 SheriBoBerry over 5 years ago

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

07051644b0d7b71abad76525ea5f4132?s=140&d=retro Matthew A over 5 years ago

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.


2 votes

permalink

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

Patty

326 points
51d48a868c1ccc030f000c3f_448215957
Submitted by
Patrícia Bailhao Wanda
almost 6 years ago


2 votes

permalink

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

211 points
32f3234938aa5ad14e114dd2f057f4d3?s=140&d=retro
Submitted by
jconte
over 5 years ago

1 Comment

07051644b0d7b71abad76525ea5f4132?s=140&d=retro Matthew A over 5 years ago

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


2 votes

permalink

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

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

490 points
52827c65f10c60fe76000bba_325014182
Submitted by
Damon Neal
over 5 years ago

4 Comments

Picture Jeremy Emerson over 5 years ago

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

923373e3c3f8d6e98ef2a5253ff28ff7?s=140&d=retro Glynys Thomas over 5 years ago

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.

Picture Joe Reitman over 5 years ago

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

Picture codecore about 5 years ago

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


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'])

946 points
52935d54abf821949d0027c8_506117216
Submitted by
Fridah Nyagu
over 5 years ago

5 Comments

5273b5caf10c6037b8002da6_627722102 Little_CodeLikeJesus over 5 years ago

how old are you fridah ?

52d8043f52f863a9ef00242d_794711961 CodeLikeJesus over 5 years ago

Recruiting old sport?

52935d54abf821949d0027c8_506117216 Fridah Nyagu over 5 years ago

does age matter really?

52d8043f52f863a9ef00242d_794711961 CodeLikeJesus over 5 years ago

Yes, yes it does old sport .

52935d54abf821949d0027c8_506117216 Fridah Nyagu over 5 years ago

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


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

258 points
1a2fbfe1f10e5c5615d192dd2707ff6d?s=140&d=retro
Submitted by
s7c
over 5 years ago

1 Comment

77e695be95fcdd06d2b3f28a8abf5b9f?s=140&d=retro 455nefer about 4 years ago

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


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.

928 points
558105a31502ecbe3e94bc24404441f5?s=140&d=retro
Submitted by
Adrian Bodolan
over 5 years ago


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?

274 points
52315fababf821d122008fe7_987637048
Submitted by
Dustin Allen Hensley
over 5 years ago

9 Comments

272d27d72a2f7fd052716eca1ef8cff0?s=140&d=retro ArronStone_521cb9f0548c35abc900012b_ over 5 years ago

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

4f84976a93d918000300de59_981251210 Michael Rochlin over 5 years ago

When I run that code it doesn't pass...

52315fababf821d122008fe7_987637048 Dustin Allen Hensley over 5 years ago

Houston, we have a bug.

52315fababf821d122008fe7_987637048 Dustin Allen Hensley over 5 years ago

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

4f84976a93d918000300de59_981251210 Michael Rochlin over 5 years ago

I'm confused... what are you submitting and what message are you getting?

52315fababf821d122008fe7_987637048 Dustin Allen Hensley over 5 years ago

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 . . .

Ee672d240647dc484f1020aeba06287a?s=140&d=retro bartekrek over 5 years ago

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)

52315fababf821d122008fe7_987637048 Dustin Allen Hensley over 5 years ago

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

5f2907a474fd7a22602b322d5d7b30c0?s=140&d=retro Alex Zhang about 5 years ago

I'm not sure


1 vote

permalink

sorry for the indentation, forgot to enclose the tag

245 points
F8f1d7d394fed739ca69c27bc0c672be?s=140&d=retro
Submitted by
Dan
about 5 years ago


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"

126 points
52fa77349c4e9d3280003e83_769694067
Submitted by
Jay
almost 5 years ago


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

191 points
7da4a9b3a611127cb9aa1ec4f9658132?s=140&d=retro
Submitted by
Gilberto Izquierdo
over 4 years ago

2 Comments

B374aab2dcbddea548e81a698a176ddb?s=140&d=retro Robert Paczyński about 4 years ago

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

Picture José Enrique Guadiana Chong almost 4 years ago

i have the exact same problems :(


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

558 points
533970729c4e9d43fc00e519_79332740
Submitted by
Ashley Amerson
over 4 years ago

1 Comment

77e695be95fcdd06d2b3f28a8abf5b9f?s=140&d=retro 455nefer about 4 years ago

Learn to add alt text. Just use


1 vote

permalink

This one is new *You are all uncodeful*

200 points
B997e9b726d35233b07df3525de4532e?s=140&d=retro
Submitted by
Kenny John
almost 4 years ago


1 vote

permalink

*You are all uncodeful*

200 points
B997e9b726d35233b07df3525de4532e?s=140&d=retro
Submitted by
Kenny John
almost 4 years ago

2 Comments

5223eb58548c353597000051_914689490 Jeffry Dale Suter over 3 years ago

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

5223eb58548c353597000051_914689490 Jeffry Dale Suter over 3 years ago

you made reall laugh. Thanks ever so much!


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)

611 points
514c65efc6e47c705b0020aa_621542609
Submitted by
Carlo DiCelico
over 3 years ago

2 Comments

514c65efc6e47c705b0020aa_621542609 Carlo DiCelico over 3 years ago

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

5223eb58548c353597000051_914689490 Jeffry Dale Suter over 3 years ago

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 getclassaverage 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"


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'])

208 points
68eb655162cdbb1a2f72f7f78c3e1cbb?s=140&d=retro
Submitted by
thatonedsm
almost 6 years ago

6 Comments

517c5073919b15e97f000e2c_202050635 Eduardo Aranguiz Olea almost 6 years ago

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

50d0937b98fba02ce20008c6_434993243 Satyam Shukla over 5 years ago

PLz try dis

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

5201146dabf8219bb9004333_770067810 kp_569 over 5 years ago

This does not work.

51306991d089e483a100728b_480979936 Omar Zaffar Khan over 5 years ago

Thanks, it worked!

51306991d089e483a100728b_480979936 Omar Zaffar Khan over 5 years ago

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

77e695be95fcdd06d2b3f28a8abf5b9f?s=140&d=retro 455nefer about 4 years ago

how do you take the average of a name?!


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

243 points
63d32770a1dd34ec89a8751f78fed49f?s=140&d=retro
Submitted by
vivek ashodha
over 5 years ago

3 Comments

52315fababf821d122008fe7_987637048 Dustin Allen Hensley over 5 years ago

Thank you much.

513ec46476ff98211500186f_711235049 Patrícia Rousseau over 5 years ago

thnks :)

513ec46476ff98211500186f_711235049 Patrícia Rousseau over 5 years ago

your get_average doesn't work :(


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.

139 points
F30fe16b518607739c54427f652dfac0?s=140&d=retro
Submitted by
xandersolis
over 5 years ago


0 votes

permalink

Why this code does not work?

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

209 points
5218abf4f10c605486002415_85224767
Submitted by
Andy_Kwok
over 5 years ago

1 Comment

52cf4aee548c355b6800072a_291139768 Paul Lukitsch over 5 years ago

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).


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 !

492 points
5273b5caf10c6037b8002da6_627722102
Submitted by
Little_CodeLikeJesus
over 5 years ago


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

207 points
9f9e861d7b19abd8865223ea19bd48f6?s=140&d=retro
Submitted by
Abdulrahman AlMesfer
over 5 years ago

1 Comment

D0a1b83eb3403df7e6260fd6f0cb35d3?s=140&d=retro Missy Lektro over 5 years ago

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


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' % getaverage(tyler)
#print 'alice average: %s' % get
average(alice)
#print 'lloyd average: %s' % get_average(lloyd)

926 points
D46900882c134b288569851b4fb75866?s=140&d=retro
Submitted by
Raymond Cater
over 5 years ago


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

163 points
Picture
Submitted by
Harman Singh Sandhu
over 5 years ago

3 Comments

Picture Harman Singh Sandhu over 5 years ago

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..

Fcad214b0250267abd1ea2c8c244f401?s=140&d=retro jackywolfson over 5 years ago

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

Fcad214b0250267abd1ea2c8c244f401?s=140&d=retro jackywolfson over 5 years ago

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


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.

254 points
52c0a4447c82cafe81000598_844431401
Submitted by
campo de fiora
over 5 years ago


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)*

950 points
58869ca42524c8927400002a_40037940
Submitted by
Tiago Menegaz
over 5 years ago


0 votes

permalink

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

221 points
53132f9c7c82ca0d4400626d_142582132
Submitted by
Julius Valsson
about 5 years ago


0 votes

permalink

this worked..

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

433 points
Bfc9f06ec6ffe05c48b2c90346a67a1c?s=140&d=retro
Submitted by
jointops
about 5 years ago


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))

146 points
E3fd2c30b57da3a041f61d151119c719?s=140&d=retro
Submitted by
zerny
about 5 years ago

2 Comments

532fa08e282ae391a4008ba5_403332393 Megan Herrington about 5 years ago

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

E3fd2c30b57da3a041f61d151119c719?s=140&d=retro zerny about 5 years ago

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


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

209 points
Picture
Submitted by
Teo Rin
about 5 years ago


0 votes

permalink

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

836 points
5342993e52f8635b57001b4e_673359343
Submitted by
Abhil lash
about 5 years ago


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 getlettergrade(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 getclassaverage(students):
results =[]
for student in students:
result = get_average(student)
results.append(result)
return average(results)

245 points
F8f1d7d394fed739ca69c27bc0c672be?s=140&d=retro
Submitted by
Dan
about 5 years ago


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.

409 points
Picture
Submitted by
Shaita
almost 5 years ago

1 Comment

548d8b10d3292f0e1d0061b7_87234459 Bittu Philip over 4 years ago

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


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)

292 points
539ea1949c4e9d094a0032cf_534749656
Submitted by
Barel
almost 5 years ago

1 Comment

77e695be95fcdd06d2b3f28a8abf5b9f?s=140&d=retro 455nefer about 4 years ago

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


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 getlettergrade(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 getclassaverage(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. getclassaverage([alice]) resulted in an error: local variable 'average' referenced before assignment

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

561 points
7cd678d66194e16066acfed918dc48f7?s=140&d=retro
Submitted by
Scott McCaig
almost 5 years ago

3 Comments

Picture Nathaniel Musial almost 5 years ago

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)?

Picture Nathaniel Musial almost 5 years ago

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

548d8b10d3292f0e1d0061b7_87234459 Bittu Philip over 4 years ago

Hi Scott ,

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


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"])

202 points
1a848f28fbd8b8c8894af07c0914adfc?s=140&d=retro
Submitted by
aj unknow
almost 5 years ago


0 votes

permalink

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

281 points
C6cc8d84dc12cd670c75a3d0cf4f238f?s=140&d=retro
Submitted by
femi_
over 4 years ago

1 Comment

B453563cbce89bf78a78c591b3a22e5a?s=140&d=retro FayusMaximus over 4 years ago

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.


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

1359 points
Picture
Submitted by
Lisa
over 4 years ago

1 Comment

36b317cd39479551b3b964a3a7288d87?s=140&d=retro duanyz over 4 years ago

"print getlettergrade(getaverage(students))" shuould be
"print get
lettergrade(getaverage(lloyd))"


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.

200 points
Picture
Submitted by
Jouke Seinstra
over 4 years ago


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.

855 points
Be01222ec55551a9864c0dccdfd66cf2?s=140&d=retro
Submitted by
nicmakaveli
over 4 years ago


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 getlettergrade(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 getlettergrade(get_average(lloyd))

466 points
548d8b10d3292f0e1d0061b7_87234459
Submitted by
Bittu Philip
over 4 years ago


0 votes

permalink

my solution:

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

730 points
19592c637e1534afb8126107b6fb354f?s=140&d=retro
Submitted by
atalexand
over 4 years ago


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)

963 points
577074801026d9c0c2000391_784614504
Submitted by
Dat Mee
over 4 years ago


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.

160 points
Ecb8f7c7d9146d49c3ae2e197d75b796?s=140&d=retro
Submitted by
William Bryce
over 4 years ago


0 votes

permalink

This worked for me:

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

be sure to indent return line

440 points
Picture
Submitted by
mondovila
over 4 years ago

1 Comment

3107a7ee58c5d3c06660f1eaf3cf586c?s=140&d=retro jaxelrod almost 4 years ago

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.


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 getlettergrade(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 getlettergrade(get_average(lloyd))

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

291 points
Picture
Submitted by
Jakhongir Yuldashevich
about 4 years ago


0 votes

permalink

This code worked for me:

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

875 points
Picture
Submitted by
Achmed Abdelaziz
about 4 years ago


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 getaverage(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
lettergrade(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=getlettergrade(a)
print result
def getclassaverage(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 getclassaverage(students)

333 points
Picture
Submitted by
Zeinab Roshdy
about 4 years ago


0 votes

permalink

Why are there so many answers?!

##And none of them work! ##

299 points
557c21f8937676baa40000c5_909167244
Submitted by
0josh0
about 4 years ago


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.

348 points
F7a6787a6d18fd82bfebbad8aefc4f1e?s=140&d=retro
Submitted by
Grega Lasnibat
almost 4 years ago


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

156 points
555230cad3292fb5c20005d7_585286822
Submitted by
JebusKryst
almost 4 years ago


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 getlettergrade(score):
if score>=90:
return "A"
elif score>=80 and score=70 and score=60 and score<70:
return "D"
else:
return "F"

#Just for testing purpose

grade=getlettergrade(get_average(lloyd))
print "Lloyd's Grade is %s" %grade

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

students = [lloyd,alice,tyler]
print getclassaverage(students)
print getlettergrade(getclassaverage(students))

138 points
7066977782add67981a8474e674e0f52?s=140&d=retro
Submitted by
Ratika Garg
almost 4 years ago


0 votes

permalink

I created an empty list oust side of the getclassaverage 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.

291 points
Picture
Submitted by
cheeto97_527875a980ff336cb500a65f_de
almost 4 years ago


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 getaverage(student):
homework = student["homework"]
quizzes = student["quizzes"]
tests = student["tests"]
return 0.1average(homework)+0.3average(quizzes)+0.6*average(tests)
def get
lettergrade(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 = getlettergrade(x)
print y
def getclassaverage(students):
results = []
for student in students:
result = getaverage(student)
results.append(result)
return average(results)
students = [lloyd,alice,tyler]
print get
classaverage(students)
print get
lettergrade(getclass_average(students))

537 points
0e2da16781f28c3e0c869c2a36b10954?s=140&d=retro
Submitted by
Azen2011_552faa97e39efe3f9c000687_de
almost 4 years ago


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

252 points
E12bf234f51b10e987f5cd390097b1a5?s=140&d=retro
Submitted by
Owen Connolly
almost 4 years ago


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 getlettergrade (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 getlettergrade(get_average(lloyd))

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

students = [lloyd, alice, tyler]

print getclassaverage(students)
print getlettergrade(getclassaverage(students))

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

323 points
Picture
Submitted by
黄炜杰
almost 4 years ago


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 getlettergrade(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 getlettergrade(get_average(lloyd))

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


183 points
1d6eb2d36331c86d7f5af8ad104f8ce0?s=140&d=retro
Submitted by
Emilio De Vincentis (Dott Grai)
almost 4 years ago


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))

888 points
7843f559bec38d799a2c7bafebc0a138?s=140&d=retro
Submitted by
Jeremy Jones
almost 4 years ago


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!

561 points
Aa35d0a140c6654528c2e7d6edbf4b97?s=140&d=retro
Submitted by
IgorARub_5484322886f5527680000acb_de
over 3 years ago


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 getlettergrade(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 getlettergrade(get_average(lloyd))

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

456 points
Picture
Submitted by
احمد السهولى
over 3 years ago