[Tutor] adding users to tweets on a list

Chris Down chris at chrisdown.name
Tue Aug 6 19:42:05 CEST 2013


On 2013-08-06 22:31, Saad Javed wrote:
> Thank you for your response. This code has a bug.
>
> If there is one user left in the user list, it doesn't print a tweet with
> just that one user added. For example use this string: "These are my
> friends living in the same city as i am. I have known them for years. They
> are good people in general. They are:"...you will see that "owais" is still
> in the list and is not added to a new tweet and printed.

Good catch, that's my bad, sorry. Because pop() is called on the last element
just before the next iteration, when `users' is coerced to a bool, it becomes
False in the outer while loop. This affects only 50% of cases because it's
possible we will be in the inner loop when we hit the last element in `users',
which is why I didn't see it.

All that needs to happen is to move the pop to the top of the MAX_LENGTH check:

    while len(new_message) + len(add) <= MAX_LENGTH:
        add = " @" + users.pop(0)
        new_message += add
        if not users:
            break
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20130806/ab41723b/attachment.pgp>


More information about the Tutor mailing list