[Tutor] adding users to tweets on a list

Saad Javed sbjaved at gmail.com
Tue Aug 6 00:48:16 CEST 2013


I want to add users to the tweet from the list, the no. of users added
based on the length of the tweet.

#!/usr/bin/env python

lst = ['@saad', '@asad', '@sherry', '@danny', '@ali', '@hasan',
'@adil', '@yousaf', '@maria', '@bilal', '@owais']

#string = raw_input('enter string: ')
#example string
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:'

limit = 140

remaining = limit - len(string)

print remaining, '\n'

running = True

while running:
    if len(lst) >=3 and len(lst[0]) + len(lst[1]) + len(lst[2]) <= remaining:
        print string, lst[0], lst[1], lst[2]
        del lst[:3]
        if len(lst) < 3:
            pass
    elif len(lst) >=2 and len(lst[0]) + len(lst[1]) <= remaining:
        print string, lst[0], lst[1]
        del lst[:2]
        if len(lst) < 2:
            pass
    elif len(lst) >=1 and len(lst[0]) <= remaining:
        print string, lst[0]
        del lst[0]
        if len(lst) < 1:
            pass
    else:
        print 'no room for friends'
        running = False

This code works but it can add a maximum of 3 users to the tweet at a
time. Is there a way to calculate how many users would fit into the
remaining space (i.e. 140 - len(tweet)) and add that no. of users from
the list to the tweet?


More information about the Tutor mailing list