tags:

views:

46

answers:

2

One of my functions returns a 'msg' object... which is merely a string.

I got into 2 for loops in the function.

msg=''

for e in example:
    msg+= "some crap"
    msg+= "some crap1"

    for sl in somelist
        msg+= v.somevalue

    msg+="-------------"

return httpresponse(msg)

There's an example of the code.

'somelist' contains two values... when the 'msg' returns it only returns the second of the two values! I'm rather confused.

A: 

If your code is exactly like that, it should work just like you want it to. However, as this clearly isn't the actual code, I'd guess you have msg = ... somewhere, when you should have msg += ... At least that's the most likely reason for the behaviour you're seeing.

If you have trouble finding where it goes wrong, put in there some "print msg" statements and test it by running your Django project in development server. You'll see where it goes wrong.

af
A: 

Your code uses sl as the loop variable, then pulls values from v. I'm not sure how they relate. If the final message includes a number of copies of the last value, then probably you forgot to relate sl and v somehow. If it includes only a single copy of the last value, then perhaps the line of code appending to msg is actually outside the loop. This would mean nothing is appended as the loop progresses, then once it exits, the last value is appended.

Ned Batchelder