#5 OK to be Average: I'm losing it, could someone gently explain?


#1

I'm getting a little less confident as this module progresses, as I know the instructions are becoming more complex and there's less hand-holding, which is great, but it does mean if you start to lose track there's no one to ask. So this is a little less about "why isn't my code working" and more "why am I being asked this?" or "what does this mean?"

The term 'float' seems to have popped up for the first time in this module, cannot recall it being mentioned before. So, what is a float?

Next, we have "use float() to convert total and store the result in total." This is where I start to read it over and over and not really understand... Use float to convert total - why? What was wrong with total? Why will being a float make it better?

So my code is fine:

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

"Way to Go! etc etc"

It's just I don't really know why it's fine, or what I did, or why I did it, and if those step-by-step instructions hadn't have been there, I could not have averaged the totals alone. I feel a fraud, like I didn't earn my little green Way to Go for that one.

Anyone fancy playing Teacher for a minute, and guiding me through what just happened? :slight_smile: Thanks.


#2

okay, short version. We have integers and float numbers. Integers are non decimal numbers (1,2,3 and so on (and -1,-2,-3 and so on)) and float are decimal numbers (1.5,2.5, 3.5 and so on (including negative numbers). Does that make sense?

you need to cast total to a float, because otherwise something might go horrible wrong. If total is a integer, and you divide a integer by a integer, you will get a integer as result. For example: 18/5 = 3 (int/int = int), but in reality the outcome is 3.6

So, your students wouldn't be really pleased with this result. (you just gave them .6 lower)


#3

@nolawildwood

-----------------------First-------------------------------

  1. You create a function called average with numbers as parameter.
  2. You set total variable to the sum of numbers
  3. You set total (which is the sum of numbers`) to a float.
  4. You set total to the quotient of float(total) divided by the length of numbers

#4

Thank you both :slight_smile: I spent the weekend going over this one until I had it straight - not just banging out code, but knowing why. I then rigged up a couple of additional lists where I wanted to force floats and integers, too, just to make sure.