displaying \n-less prompts in a pythonic way

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alf

    #1

    displaying \n-less prompts in a pythonic way

    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.writ e('enter command:') instead but kind of do not
    like sys.stdout.writ e mixed up with print's statements used to display
    other informations.


    Is there a pythonic solution for the problem?

    Thx, alf
  • Sybren Stuvel

    #2
    Re: displaying \n-less prompts in a pythonic way

    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.writ e('enter command:') instead but kind of do not
    like sys.stdout.writ e 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.writ e(label)
    return sys.stdin.readl ine()

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

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: displaying \n-less prompts in a pythonic way

      Sybren Stuvel:
      def prompt(label):
      '''Prompts the user, returning the typed text'''
      sys.stdout.writ e(label)
      return sys.stdin.readl ine()
      Maybe raw_input function may help too.

      Bye,
      bearophile

      Comment

      • Steve Holden

        #4
        Re: displaying \n-less prompts in a pythonic way

        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.writ e('enter command:') instead but kind of do not
        >>like sys.stdout.writ e 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.writ e(label)
        return sys.stdin.readl ine()
        >
        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

        Comment

        • alf

          #5
          Re: displaying \n-less prompts in a pythonic way

          Steve Holden wrote:
          Or use raw_input(), which was designed for such situations:
          >
          thx, did not know about that ...

          Comment

          • Hendrik van Rooyen

            #6
            Re: displaying \n-less prompts in a pythonic way

            "Steve Holden" <steve@holdenwe b.comwrote:
            8<-----------------------------------------------------------
            >>mystr = raw_input("Who is this? ")
            Who is this? Steve
            how did you know how to answer that?

            - Hendrik


            Comment

            Working...