-2

I've tried to create a generator function which can find simple moving average of the list, but it doesn't work at all. I think the root of the problem might be in finding sum

def gen(lst, n):
    s = 0
    for i, _ in enumerate(lst):
        if lst[i] < n:
            s += lst[n - (n-i)]

my code does nothing when i play it. What should I do?

JF_01
  • 21
  • 4
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results differ from what you expected. We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. – Prune Nov 20 '20 at 18:09
  • Nothing happens because you never call the generator. We have little idea how you expect this to work, because you've used meaningless variable names and have provided no calling program or documentation. You seem to be confused about the difference between a list and a list element. You will need to explain your "moving average" concept, because what you implemented is something different than the usual idea. – Prune Nov 20 '20 at 18:11
  • 1
    you forgot creating loop for variable i. – Pygirl Nov 20 '20 at 18:11
  • 1
    Most of all, it appears that you've avoided testing any of your code until this point; you now seem to have multiple problems at once, which will make it harder to debug. Please trace the operation with strategic `print` statements to reduce this to a single question. See this [lovely debugging site](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for help. – Prune Nov 20 '20 at 18:12
  • You never defined `i`, and you're not showing how you're calling your generator. We don't have enough information to go off of, so the answers here are going to be shots in the dark. – Random Davis Nov 20 '20 at 18:13
  • 1
    this will give you an idea how to create moving avg in python: https://stackoverflow.com/a/13732668/6660373 – Pygirl Nov 20 '20 at 18:14
  • Do you need your own code fixed, or do you merely need your problem solved? If all you need is moving-average code, then this question is off-topic because you didn't look up the solution on your own. We expect you to do such research before posting here. – Prune Nov 20 '20 at 18:14

2 Answers2

0
if lst[i] < n:

You do not define i

but I guess i is a index of lst so try:

for i, _ in enumerate(lst):
  if lst[i] < n:
    s += lst[n - (n-i)]

EDIT:

def gen(lst, n):
    if n > len(lst):
        print("Sorry can't do this")
    try:
        for i, _ in enumerate(lst[:-(n-1)]):
            s = sum(lst[i:i+n])
            yield s/n
    except ZeroDivisionError:
        print("Sorry can't do this")
JLeno46
  • 1,186
  • 2
  • 14
  • 29
0

You may have some issues with this code snippet because you did not include some relevant variables and how they are used.

Below is an example of how you can calculate a moving average of list_of_numbers, given a position p with a period of n.

def mAverage(list_of_numbers, p, n):
    if n < p:
        return sum(list_of_numbers[p-n:p])/n
    else:
        return sum(list_of_numbers[0:p])/(p + 1)

Note: At the beginning of the list this function will only calculate the moving average with a period equal to the position in the list.

shn
  • 865
  • 5
  • 14