Python Forum
Need Help for Project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help for Project
#1
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.
Reply
#2
what have you tried? post your code in python tags and full traceback in error tags if you get any.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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]
Reply
#4
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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]
Reply
#6
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?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(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]
Reply
#8
Wait until you get to computer and post the code as wavic suggested. You are not doing your project on a mobile, are you?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020