473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

displaying \n-less prompts in a pythonic way

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
Oct 26 '06 #1
5 1144
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/
Oct 26 '06 #2
Sybren Stuvel:
def prompt(label):
'''Prompts the user, returning the typed text'''
sys.stdout.write(label)
return sys.stdin.readline()
Maybe raw_input function may help too.

Bye,
bearophile

Oct 26 '06 #3
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

Oct 26 '06 #4
alf
Steve Holden wrote:
Or use raw_input(), which was designed for such situations:
thx, did not know about that ...
Oct 26 '06 #5
"Steve Holden" <st***@holdenweb.comwrote:
8<-----------------------------------------------------------
>>mystr = raw_input("Who is this? ")
Who is this? Steve
how did you know how to answer that?

- Hendrik
Oct 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave Posh | last post by:
I seem to be having a problem displaying time stored in mysql. The format stored in the database is 13:15:05. The database data type is time. I'm using asp vbscript and sql to retrieve the time...
1
by: Dave Posh | last post by:
I seem to be having a problem displaying time stored in mysql. The format stored in the database is 13:15:05. The database data type is time. I'm using asp vbscript and sql to retrieve the time...
2
by: Carole MacDonald | last post by:
There have been lots of posts on this topic, but I haven't been able to apply any of the suggestions to my situation. I have an .aspx page with a form on it that has several submit buttons on...
5
by: Robert | last post by:
Hello Accessors I have some reports created in Access that are very good for what they do. However, it seems to me that when you are displaying information you don't need to print out that a...
2
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.