Problems with user input

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

    #1

    Problems with user input

    I have some easy questions, but I cant figure them out:

    I want to read exactly one char from stdin, there should
    be no need to hit enter.

    import sys

    print "Input: (y/N) ",
    input = sys.stdin.read( 1)
    print "Your input:", input

    This code has following problems:
    1) I need to hit enter, although I read only one char?!?
    2) The complete output is:

    Input: (y/N) y
    Your input: y

    Why does the whitespace char appear in front of "Your input..."?
    I guess it has something to do with not causing a linebreak
    after "Input: (y/N) ", but I want no linebreak...

    Any suggestions?

    Regards,
    Florian

  • F. Petitjean

    #2
    Re: Problems with user input

    On Tue, 09 Nov 2004 23:54:37 Florian Wilhelm <Florian.Wilhel m@web.de> wrote:[color=blue]
    > I have some easy questions, but I cant figure them out:
    >
    > I want to read exactly one char from stdin, there should
    > be no need to hit enter.
    >
    > import sys
    >
    > print "Input: (y/N) ",
    > input = sys.stdin.read( 1)
    > print "Your input:", input
    >
    > This code has following problems:
    > 1) I need to hit enter, although I read only one char?!?[/color]
    sys.stdin is : buffered and line oriented.
    What is your operating system ? Under unix sys.stdin (and C stdin
    stream) are managed by a driver which expect you can somewhat modify
    the line you want to send to the system. (POSIX normalized)
    If you want a fine control, you can use curses (Unix) or build your
    solution based on getchar (or is it getch() ?) on windows/dos.[color=blue]
    > 2) The complete output is:
    >
    > Input: (y/N) y
    > Your input: y
    >
    > Why does the whitespace char appear in front of "Your input..."?[/color]
    ??[color=blue]
    > I guess it has something to do with not causing a linebreak
    > after "Input: (y/N) ", but I want no linebreak...
    >
    > Any suggestions?[/color]
    Tell us more about what you want to do.[color=blue]
    >
    > Regards,
    > Florian
    >[/color]

    Comment

    • Diez B. Roggisch

      #3
      Re: Problems with user input

      > I want to read exactly one char from stdin, there should[color=blue]
      > be no need to hit enter.[/color]

      That's because your terminal is in canonical mode - that means that it only
      reads whole lines. You can put it to non-canonical mode - one way is
      described here:


      http://www.ibiblio.org/obp/py4fun/lode/lode.html#auto2

      Please make sure you set it back to canonical mode, or you will expirience
      some strange effects.
      [color=blue]
      > Why does the whitespace char appear in front of "Your input..."?
      > I guess it has something to do with not causing a linebreak
      > after "Input: (y/N) ", but I want no linebreak...[/color]

      The print statement concats an argument list by spaces. Use this instead:

      sys.stdout.writ e("Your input:%s\n" % input)

      --
      Regards,

      Diez B. Roggisch

      Comment

      • Florian Wilhelm

        #4
        Re: Problems with user input

        My OS is Linux 2.6.8.1 and Python version 2.3.4

        I just want to write a function that, if called,
        asks a yes/no question. The user should only need
        to type one char, without confirming with enter.

        I wrote a function similar to following

        import sys

        print "Input: (y/N) ",
        input = sys.stdin.read( 1)
        print "Your input:", input

        If I execute these lines and type "y" followed by "enter".
        I get this output

        Input: (y/N) y
        Your input: y

        The problem is that there is a whitespace in front of the
        second line, that is not generated by the second print
        statement. I dont understand why it appears...

        It really has nothing to do with the second "print",
        because following code:

        import sys

        print "Input: (y/N) ",
        input = sys.stdin.read( 1)
        print "Hello World"
        print "Your input:", input

        produces this output...

        Input: (y/N) y
        Hello World
        Your input: y

        Can somebody explain why the whitespace char appears
        in front of "Hello World"???

        Comment

        • Steven Bethard

          #5
          Re: Problems with user input

          Florian Wilhelm <Florian.Wilhel m <at> web.de> writes:[color=blue]
          >
          > import sys
          >
          > print "Input: (y/N) ",
          > input = sys.stdin.read( 1)
          > print "Hello World"
          > print "Your input:", input
          >
          > produces this output...
          >
          > Input: (y/N) y
          > Hello World
          > Your input: y
          >
          > Can somebody explain why the whitespace char appears
          > in front of "Hello World"???[/color]

          This is the behavior of the print statement. For example:
          [color=blue][color=green][color=darkred]
          >>> print "X", sys.stdin.read( 1), "Y"[/color][/color][/color]
          X1
          1 Y

          Every comma between two expressions in the print statement adds a space. As you
          can see from the code above, the space isn't inserted until after the following
          expression (sys.stdin.read (1)) is executed, and hence I get the space from the
          print statement following the newline I inserted when I pressed <ENTER> after
          typing 1.

          If instead, you use sys.stdout.writ e, you get:
          [color=blue][color=green][color=darkred]
          >>> def f():[/color][/color][/color]
          .... sys.stdout.writ e("X%s" % sys.stdin.read( 1))
          .... sys.stdout.writ e("Y\n")
          ....[color=blue][color=green][color=darkred]
          >>> f()[/color][/color][/color]
          1
          X1Y

          sys.stdout.writ e doesn't insert spaces (unless you put spaces in the strings).
          In general, if you want a particular format, you should use sys.stdout.writ e
          instead of print, since the print statement has a number of such idiosyncracies.

          Steve



          Comment

          • Florian Wilhelm

            #6
            Re: Problems with user input

            Thank you very much!

            That solved both problems!

            Comment

            • Terry Reedy

              #7
              Re: Problems with user input


              "Florian Wilhelm" <Florian.Wilhel m@web.de> wrote in message
              news:pan.2004.1 1.09.22.54.37.1 20023@web.de...[color=blue]
              > I want to read exactly one char from stdin, there should
              > be no need to hit enter.[/color]

              Your problem is that terminal input, as seen from C, is usually, by
              default, in buffered line mode. This allows the user to edit entries, at
              least by back-spacing, before hitting enter. How to switch to character
              mode or grab the raw keyboard input is OS specific.
              [color=blue]
              > Input: (y/N) y
              > Your input: y
              >
              > Why does the whitespace char appear in front of "Your input..."?[/color]

              Because the print statement puts spaces between items so you don't have to.
              If the convenience of print does not do what you want, take control by
              using sys.stdout.writ e(<string>), which writes *exactly* what you tell it
              to.

              Terry J. Reedy



              Comment

              Working...