displaying \n-less prompts in a pythonic way

A

alf

Hi,

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?

Thx, alf
 
S

Sybren Stuvel

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
 
S

Steve Holden

Sybren said:
alf enlightened us with:



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:
Who is this? Steve

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top