Python Tutorial 10: Homework Solution for Averaging Numbers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter from toptechboy.com and we're here today with lesson number 10 in our incredible new tutorial series where you are gonna learn python or you're gonna die trying what i'm going to need you to do is pour yourself a nice strong cup of coffee that is straight up black coffee no sweeteners no sugar no creamers none needed you guys ought to know the routine by now this is the way you got to look at it you guys watching this channel you're like jet fighter aircraft and what a jet fighter aircraft run off of they run off a jet fuel they don't run off of dr peppers they don't run off of coca-colas they don't run off of soda pop they run off of jet fuel you guys your jet fighter aircraft and you run off of your your jet fuel is strong black coffee if i catch you putting sugar in your coffee you will get banned from this channel but anyway i have digressed needs you to get out also your visual studio code and get ready to learn some cool new stuff as you're firing up your old visual studio code as always i want to give a shout out to you guys who are helping me out over at patreon it is your encouragement and your support that keeps this great content coming and keeps me in premium coffee beans you guys that are not helping out yet look down in the description there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's jump in and let's look at what we are going to do today i do believe in lesson number nine i gave you a homework assignment and that homework assignment was to input a list of grades from the user and then average those grades and print out the individual grades and print out the average were you able to do it if you were able to do it leave a comment down below i am legend if you are not able to do it leave a comment i folded up like a cheap walmart lawn chair did you guys hold up like a cheap walmart lawn chair or were you able to do this assignment man you guys have got to get to the point that you're struggling through this because when i do it it looks easy and when you do it along with me it looks easy and when you do it along with me you think you understand it but then when you go to do something on your own you just get wrapped around the axle so that's why i want you to like spend at least an hour trying to do these assignments and then if you absolutely positively can't do them then watch me do them is oh yeah i see what he's doing i'm understanding it but if you struggle with something for an hour and then watch me do it then you really will learn but if you're just cruising along with your soda pop thinking that you're a coder because you can copy me you're just kidding yourself all right but enough of this nonsense let's switch over and let's go to our most excellent visual studio code and then what we are going to need to do is we are going to create a new program which is called average grades dot p y dot p y and that dot p y is kind of important it's actually very important and then i am going to come over here now some of this you guys should have been able to do without much trouble if you are going to input grades the first thing you got to know from the user is how many grades there are and so we're going to call that num grades and you'll get that from the user what are you getting you're getting a number and so we better tell it it's a number and since we're going to be using this as an index we better make it an int because it's kind of like first grade second grade third grade fourth grade we don't want there's no such thing as the 1.5 grade so we want it to be an int and then we're going to input it from the user and then we're going to give them a nice prompt how many grades do you do you have have a question mark and a space and then this uh apostrophe this single quote closes the string first parentheses closes the input and second parentheses closes the int and this is like one of the things in coding as you get to more and more complicated coding you've got to always make sure that your parentheses balance because when they don't sometimes you get a really strange error and it might be on the next line of code and you'll go crazy so i always just take a moment and verify that i am closing all the things that i'm opening i'm also going to get rid of the file viewer because it will allow you to see things better okay so now i know how many uh how many grades that my user wants and so now i'll put it in a for loop for i in range and where do i want to start i want to start at zero and then i want to go to num grades and i want to go in increments of 1. now just think if grade says 0 you would start at grade 0 grade 1 grade 2 so for 3 grades you go grade zero grade one grade two and so you're stopping it an index of two which is one less than numb grades but that's how the range function works it stops at one number less than where you told it to go so even though it does that it all works out because you're starting at zero i'm not going to belabor that point if you don't understand it go back and watch last week's video again i hope i didn't sound grumpy i can't sound kind of grumpy there it's just you got to get this in your head and if you don't understand it make sure you go back to the earlier video so that you can understand now what am i going to want to do well i'm going to say grade the individual grade is equal to now this is going to be a float all right i'm going to get it as a float and i'm going to get it as an input and then i'm going to say please please enter your please enter your grade and then colon space so that it'll look nice now that just reads the grade in one at a time and when i get the next grade it overwrites the old grade and so i need to save these in an array in an array because if i just put them in grade i'm only going to end up with the last grading grade i'm going to have lost them all so i have to put them in an array well if i'm going to put them in an array i've got to create that array up here so i will call that grades the list of grades and putting the brackets like that it makes it a list or an array and that will hold all my grades and now what i have to do is i have to append the individual grade grades into the array grades and how do i do that well the array is grades and i want to dot append and what do i want to append the individual grade grade okay now if i am not mistaken and i don't think i am at this point i should have all the grades in an array now just for good measure i'm going to go ahead and print them out just to make sure that i got the grades in because i like to run my code little pro tip don't write 100 lines of code and then try to run it as you get little functions working little blocks working go in and make sure that those little smaller parts are working so now i'm going to say print and i'm going to say your your grades are and i only want to print that one time so i don't want that inside the for loop and so i come back right unindented so this print is not in the for loop it'll just do it one time but now i want to print out those grades so i will say 4i in range starting where starting zero go up to num grades but stop one before and then increment of one and then a most excellent colon and then what i'm going to do is print what i'm going to print grades plural grades plural of i and so remember i is my index and so the first time through i is zero the second time through i is one the third time through i is two and then uh it will print out those grades that i put in here so let's just make sure that we can get the grades in and print them out before we start trying to do anything really fancy so let's come over here and let's run this thing and it's asking me for the number of grades i'm going to say i have three grades my grades are 90 95 and 100 and then let me make sure you can see this it comes up and it says your grades are and then it says 90 95 and 100. so i got the first grade i got the last grade all this nonsense with from zero to num grades and increments one that is all working and this is kind of what we did last week but it's always good to review it but now what i need to do is average the grades so were you guys able to do this how do you average grades you add all the grades together and then you divide by the number of grades so how would i add all the grades together well i have to step through all the grades and add them how would i step through well i need another for loop so here i have input the grades and then here i have printed out the grades but now i need to average them and so what i'm going to do is i'm going to step through them for i in range starting with 0 because that first number is in the zero position up to num grades stop the one before it in increments of one and now what do i want to do i want to add all the grades together how would i do that well i need a new variable i need a new variable i'm going to call it my bucket now is used a variable called bucket now as you step through the array grades what do you need to do with each individual grade you got to say it very careful because if you say it wrong you're going to program it wrong and i hope you didn't say put the grade in the bucket because that would be like bucket is equal to uh and let's say uh do i really want to do i yeah i'll do i bucket is equal to grades of i grades of i right now if you do this what are you doing you're doing what you said which is you're putting the number in the bucket you're putting the next grade in the bucket you're putting the next grade in the bucket what is wrong with that well when you put the first grade in then you come and you put the next grade in it erases the first grade you're just one at a time putting the numbers in the bucket so you've got to say it right what do you want to do with each number you want to take each number and you want to add it to the bucket you want to add it to the bucket here i just put it in the bucket but what do you want to do you want to add it to the bucket so you want to add grades of i to the bucket what is the mathematical operator add plus okay what are you adding grades of i and what are you adding it to you're adding it to bucket okay so bucket is going to be equal to whatever was in the bucket before plus the next grade then when you go it gets the next grade and it adds it to the bucket and then when you get all the way through you have the sum of all the grades and they are contained in your bucket now this is so incredibly easy but if you haven't done it before it's really hard to get your mind around it but bucket is equal to buckets plus grades of i now when i get out of this i've got a bucket and the bucket is the sum of all the grades now what do i want to do well i'm going to say my average is equal to bucket divided by num grades now we have to remember numb grades is actually the number of grades now there was some foolishness going on up here about you know starting at zero and stopping one before numb grades but still always numb grades is actually my number of grades so i take bucket divided by num grades now one of the things is i want you to think you add all the numbers up and then you divide by the number of grades how many times do you divide by the number of grades once one of the most common mistakes that i see students make is they put this average this dividing by numb grades inside the for loop and they're dividing by num grades dividing by number grades dividing by dumb grades and they get some tiny little number okay you don't divide by num grades every time through you generate the sum and then after you generate the sum you come out of the for loop and then you divide one time by num grade so that should not be in there all right so now what we are going to do we are going to say print and let's just print a blank line just to make it kind of look nice and then and then let's say print and then your average is and then we will put a comma and then average like that this is pretty cool isn't it i hope you guys hope some of you guys were able to figure this out need a little jet fuel here throat's a little dry a little parched we need jet fuel okay so let's run this sucker i'm going to clear out that terminal come up here and run it i guess i'm happy that it didn't die right off the bat and so i'm going to say three grades please enter your grade 90 95 and 100 and denied it says okay all right now now you got to think about something it says and this is also one of the most common errors i see it says in line 10 you have an error in line 10 is when you said bucket is equal bucket plus grades it said bucket is not defined now why is that because it's looking at the right and it says i want to take bucket i want to add grades to it and put it in bucket but it hasn't seen bucket before so it says bucket doesn't have a value okay so you see you've got to initialize bucket and you do this up at the top here so remember how i told you how much trouble you get into because you don't have to declare your variables well this is an example so i just need to say bucket is equal to what's in the bucket to begin with nothing the bucket's empty so start with an empty bucket and then when you get down here it's gonna the first time through say bucket is equal to bucket which is zero it knows it's zero so it's okay and then plus the first grade or the zeroth grade to be more precise and now bucket is equal to the zeroth grade and then the next time through bucket is going to be the 0th grade plus the first grade and then it will just go through summing them up rookie mistake there actually i did not do that on purpose and as many times as i've seen student make that i'm surprised that i made it but little jet fuel here will get me going again nothing like pure jet engine petrol yeah all right so here we go okay how many grades do i have i'm going to say 3 and then i'm going to say 90 and then i'm going to say 95 and then i'm going to say 100 and then boom double chest bump you know the professional football players they give this to each other i think us engineers ought to do this because knowing how to average grade i think is a lot more important than that nonsense out on the football field so it says please enter your grades it says your grades are 90 95 100 and it says your average is 95. okay now we need to check around a little bit more than just that so i'm going to run it one more time and i'm going to say three grades and i'm going to say a 90 okay and then i'm going to say a 91 and then i'm going to say a 93 i just want to see if it works for integer value or for you know non-integer type numbers and yes your average is 91.33333333333 okay that's a little bit annoying and so i wonder i'm not going to try to do i'll do a separate lesson on that how to format print statements a little better i don't want to try to do that on the fly but you can see that we have this working right does it make sense did it help even if you failed doing it on your own did it help struggling through it for some time and then watching my solutions i hope it did okay so i'm trying to get you guys to think like a programmer i'm trying to get you to independently think through these kind of classic problems and so i'm going to go ahead and i'm going to give you a homework assignment for next week and what the homework assignment for next week is is to take this average grades and i think next week i'll just start with this program i won't re redevelop it from scratch but next week what your homework is is to input the grades print the grades average the grades and then tell the user what his or her her high grade was and what his or her low grade was okay high grade or you know average high grade and low grade think about it probably going to take another for loop or two okay you might have to have a for loop to find the high grade you might have to have a for loop to find the low grade okay guys i am having so much fun with this i hope i'm not boring you but man you know what the thing that happened to me was was that these were so hard to learn and so there's a tendency to just go out and find someone else's solution but if you think through it yourself you start really developing an engineering mind and a programming sort of frame of reference and i really want you to learn to code in these lessons and so i hope that they are helping you okay guys uh you know really appreciate it if you enjoyed the video to give me a thumbs up because that really helps me with the old youtube algorithms and then subscribe to the channel when you do make sure you ring that bell so that you get notifications when future lessons come out and then share this with other people so that we get more doers in the world too many watchers in the world we need more doers and less watchers so help somebody become a doer by introducing them to this most excellent channel alright guys this is paul mcquarter from toptechboy.com i will talk to you guys next week ah that's good
Info
Channel: Paul McWhorter
Views: 8,370
Rating: undefined out of 5
Keywords:
Id: tia1zxnmcFs
Channel Id: undefined
Length: 20min 40sec (1240 seconds)
Published: Wed Apr 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.