On 12/08/2019 17:54, Marissa Russo wrote:

> def mean(nums):
>     for num in nums:
>         _sum += num
>     return _sum / len(nums)
> 
> def mean2(nums2):
>     for num in nums2:
>         _sum += nums2
>     return _sum / len(nums2)
> 
> def main():
>     data = get_numbers()
> 
>     print("The mean of the first file is: ", mean)
>     print("The mean of the second file is: ", mean2)

Notice that in the print statement you use the names
of the functions.
Python therefore evaluates those names and discovers
that they are functions, so that's what it tells you.

To get the values you need to call the functions,
which you do by adding parens to the name:

     print("The mean of the first file is: ", mean(data[0]) )
     print("The mean of the second file is: ", mean2(data[1]) )

That will then execute the functions....

Which leads us to the next problem.
In both functions you have a line like:

_sum += num

But that expands to

_sum = _sum + num

Which means you are trying to get a value from _sum
before assigning any value. You need to initialize
_sum before using it like that.

_sum = 0

But better still would be to use the builtin sum method:

sum(nums)

So your mean function turns into:

def mean(nums):
   return( sum(nums)/len(nums))

But that leads to the next problem which is that your data
is still in string format, which is what you read from the
file with getlines().
You can convert it to integers using a list comprehension
inside your get_numbers function:

nums = [int(s) for s in nums]

which assumes the file is a list of numbers each on one line?

If you are not familiar with the list comprehension shortcut
you can do it longhand like this:

num_copy = []
for num in nums:
    num_copy.append(int(num))
nums = num_copy

Incidentally, the two mean functions look identical
to me. What do you think is different other than
the names?

Finally, you could just use the mean() function defined in
the statistics library of the standard library.

import statistics as stats
...
     print("The mean of the first file is: ", stats.mean(data[0]) )
     ...

That should be enough to be getting on with.

Once you get those things done you could post again and
we might suggest some ways to tidy the code up a little.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to