1

I have a bit of code which I think is correct. However, it should return a vector of values but it returns a single cumulated value instead.

pExact = 0.8
pOvershoot = 0.1
pUndershoot = 0.1

def move(p, U):
    q = []
    for i in range(len(p)):
        s = pExact * p[(i - U) % len(p)]
        s = s + pOvershoot * p[(i - U - 1) % len(p)]
        s = s + pUndershoot * p[(i - U + 1) % len(p)]

        print i, s  # check if calculations are corrects
    q.append(s)

    return q        # should return a vector

p = [0, 1, 0, 0]
print move(p, 1)    # prints only cumulated value

I try to understand why it prints only one value [0.10000000000000001] not a vector [0, 0.1, 0.8, 0.1] how I think it should.

2
  • 3
    Your q.append(s) belongs into the for loop? (indent it by one step to the right) Commented Sep 19, 2012 at 8:47
  • Why the downvote ? this is a perfectly structured question with a perfectly obvious answer Commented Sep 19, 2012 at 8:50

3 Answers 3

3

There's a wrong identation in q.append(s). It should be inside for loop.

Here is the correct version:

pExact = 0.8
pOvershoot = 0.1
pUndershoot = 0.1

def move(p, U):
    q = []
    for i in range(len(p)):
        s = pExact * p[(i - U) % len(p)]
        s = s + pOvershoot * p[(i - U - 1) % len(p)]
        s = s + pUndershoot * p[(i - U + 1) % len(p)]

        print i, s  # check if calculations are corrects
        q.append(s)

    return q        # should return a vector

p = [0, 1, 0, 0]
print move(p, 1)    # prints only cumulated value
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. Sometimes the easiest thing is most difficult to spot.
No worries, that is because pair programming is a winner. :-)
@tomaszz74 you should also check my post for yield, it's preferred a lot over the array approach
2

Instead of using q, use the more pythonic yield

def move(p, U):
    for i in range(len(p)):
        s = pExact * p[(i - U) % len(p)]
        s = s + pOvershoot * p[(i - U - 1) % len(p)]
        s = s + pUndershoot * p[(i - U + 1) % len(p)]

        print i, s  # check if calculations are corrects
        yield s

Also, the problem with the code is that you forgot 4 spaces indentation before q.append(s)

2 Comments

I try to yield as you propose, and the output is: <generator object move at 0x7f9476123320> can you please give me some more light about what's going on?
yield semantics are a little different from regular arrays, you can find more about them here. I really advise you to take a look at them and use them wherever possible/useful.
1

It's just an indentation problem. Your line q.append(s) is at the same indentation level as the main part of the function, which means it only executes after the end of the for loop. Move it one level to the right, so it goes with the rest of the body of the loop, and it'll be executed each time through the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.