displaying \n-less prompts in a pythonic way

Steve Holden steve at holdenweb.com
Thu Oct 26 09:10:28 EDT 2006


Sybren Stuvel wrote:
> 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()
> 
Or use raw_input(), which was designed for such situations:

  >>> mystr = raw_input("Who is this? ")
Who is this? Steve

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list