Jorge Godoy <[EMAIL PROTECTED]> wrote:

> See if this isn't better to read:
> 
>========================================================================
>======== 
> def print_message(some_str):
>     if some_str.startswith('track='):
>         print "Your track is", some_str[6:]
>     elif some_str.startswith('title='):
>         print "It's a title of", some_str[6:]
>     elif some_str.startswith('artist='):
>         print "It was played by", some_str[7:]
>     else:
>         print "Oops, I dunno the pattern for this line..."
>     return
> 
> for line in ['track="My favorite track"', 'title="My favorite song"',
>              'artist="Those Dudes"', 'Something else']:
>     print_message(line)
>========================================================================
>======== 
> 
> Expected output:

Or you could make it even clearer by using a loop:

messages = [
        ('track=', 'Your track is'),
        ('title=', "It's a title of"),
        ('artist=', "It was played by"),
        ]

def print_message(s):
        for key, msg in messages:
                if s.startswith(key):
                        print msg,s[len(key):]
                        break
        else:
                print "Oops, I dunno the pattern for this line..."

                
for line in ['track="My favorite track"', 'title="My favorite song"',
             'artist="Those Dudes"', 'Something else']:
    print_message(line)

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to