Question about text in Python

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Mon Apr 2 05:41:05 EDT 2007


On Mon, 02 Apr 2007 18:48:00 +1000, Steve wrote:

> Hi,
> 
> I've created a Python program that a user enteres one line of text which 
> will then create an acronym from that text.
> 
> What I want to add to the program is the abilty to rerun this process (where 
> the user enteres another line of text) until the user has had enough and 
> enters a blank line which would then end the program.
> 
> Can anyone help with this?

It isn't really a question about _text_. It's a question about programming
techniques.

Firstly, break the task into small pieces:

* get input (a line of text) from the user;
* make an acronym from the text;
* decide if you're done;
* if not, repeat from the beginning.

Now write code to do each piece.

Now all you need to do is create functions to do each sub-task:

def get_input():
    """Get input from the user."""
    return raw_input("Please type a sentence. ")


def make_acronym(text):
    """Return an acronym from the given text."""
    # you have already done this
    .... do stuff ...
    return acronym


def are_we_done():
    """Return a boolean flag indicating whether 
    we're done making acronyms.
    """
    print "Would you like to make another acronym?"
    print "Type Y or y for yes, N or n for no."
    result = raw_input("Then press the Enter key. (y/n?) ")
    return result in 'yY'


Now put them all together into a loop.


done = False # we're not done yet
while not done:
    # repeat until we're done
    text = get_input()
    acronym = make_acronym(text)
    print acronym
    done = are_we_done()


There are lots of other ways of doing this. This one is (I think) the
easiest to understand, but there are other ways that are more
efficient but use more advanced concepts like exceptions. Have fun
exploring the different ways of doing this task.




-- 
Steven D'Aprano 




More information about the Python-list mailing list