Python Forum

Full Version: Need Help for Project
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Basically i am working on a project.Let me explain the project.You are gonna write some values as much as you want on a txt file.Then the code will calculate the arithmetic mean of these values.Then we are gonna substract the arithmetic mean from the values we entered one by one.For example if the value we entered is xi we will get xi-arithmetich mean.Then (xi-arithmetich mean)^2 ,(xi-arithemtic mean)^3 ,(xi-arithmetic mean)^4 and finally we will calculate the arithmetic mean of these new values.If you can tell me the basics about reading the values from text and writing a function for unknown number of values.If value number was given for example 10 value i could write it but i dont know how to write for unknown number of values.
what have you tried? post your code in python tags and full traceback in error tags if you get any.
(May-10-2018, 08:28 AM)buran Wrote: [ -> ]what have you tried? post your code in python tags and full traceback in error tags if you get any.


[Image: 6oHzTyV]
Getting the values from the file:
with open('values.txt', 'r') in_file: # opens a file.
    for value in in_file: # reads every line from the file
        # turn value into a number because it is a string
        # add the number to a list
Defining a function:
def my_func():
    # code block here - indented.
    return data # the return statement returns the result produced by the function. It's not mandatory. If there is no return statement
    # the function returns None.
Think of how do you calculate the average for a bunch of numbers and try to do in Python. Give it a try.
For example if you look at the picture i can calculate the values in this condition.But the function needs to calculate the newnumbers by his own for unknown number of values.It needs to calculate them for for example 2 values or 50 values.I couldnt figure out how to write it.[Image: h4jarJ]
The function gets the already extracted from the file values as an argument. Or you can get the values in the same code block. The length of the list will give you the number of the values and the sum function will give you the... sum. It will work for an arbitrary number of values. That's all you need. I don't see any picture. Please post your code between code tags. We can't run a picture here.

What new numbers? Are they all stored in the file or there is some user input?
(May-10-2018, 09:34 AM)wavic Wrote: [ -> ]The function gets the already extracted from the file values as an argument. Or you can get the values in the same code block. The length of the list will give you the number of the values and the sum function will give you the... sum. It will work for an arbitrary number of values. That's all you need. I don't see any picture. Please post your code between code tags. We can run a picture here.

What new numbers? Are they all stored in the file or there is some user input?

I am on mobile right now.You can see this?

[Image: A19aR7.jpg]
Wait until you get to computer and post the code as wavic suggested. You are not doing your project on a mobile, are you?