[Tutor] python calculate number in a text file

Alan Gauld alan.gauld at yahoo.co.uk
Tue Oct 6 19:50:18 EDT 2020


On 06/10/2020 17:10, Fabian Lunell wrote:
> You can here download
> <http://homepage.lnu.se/staff/jlnmsi/python/2020/10000_integers.zip> two
> files each containing 10000 integer values. The main difference between the
> two files is how the integers are formatted in the file.
> 
> Write a program read_numbers.py that reads the two files (one after each
> other) and for each file computes and presents the average (mean) value and
> the standard deviation. We expect two separate functions mean(lst) and
> std(lst) that computes the mean and standard deviation for a given list of
> integers lst. Here you can find more information about the standard
> deviation <https://www.mathsisfun.com/data/standard-deviation-formulas.html>

Thats an interesting exercise which most of us here already
know how to do it and don't have much motivation to do it.
I assume its a homework and you would like some help?
But you don't ask any questions... or show us any code.

We won't do your homework for you but we will offer assistance
if you show us your work, and we can suggest ways to progress.

For example, there is a statistics module in the python
standard library.

In the meantime do you know how to read data from a file?
Do you know anything about the different data formats?
How to read them into a consistent data format in your program?

I'd expect your code to be structured like this:

def mean(lst):....
def std(lst):....

for file in <your two filenames>
    numlist = read data from file
    print(file, "mean: ", mean(numlist))
    print(file, "SD: ", std(numlist)

If you have specific questions get back to us, include any
error messages in full please.

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




More information about the Tutor mailing list