displaying \n-less prompts in a pythonic way

Sybren Stuvel sybrenUSE at YOURthirdtower.com.imagination
Thu Oct 26 08:21:04 EDT 2006


alf enlightened us with:
> I have a command line program which also does some interaction with the 
> user using stdin and stdout.
>
> My requirement is to print prompt so the user can answer in the same 
> line. Unfortunately:
>
>   print 'enter command:',
>
>
> does not really work as the comma is carried over to the following lines 
> and the indentation gets messed up.
>
>
> I can use sys.stdout.write('enter command:') instead but kind of do not 
> like sys.stdout.write mixed up with print's statements used to display 
> other informations.
>
>
> Is there a pythonic solution for the problem?

Yeah, write a function:

def prompt(label):
    '''Prompts the user, returning the typed text'''

    sys.stdout.write(label)
    return sys.stdin.readline()

Sybren
-- 
Sybren Stüvel
Stüvel IT - http://www.stuvel.eu/



More information about the Python-list mailing list