A completely silly question

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

    #1

    A completely silly question

    This must be the silliest question ever:

    What about user input in Python? (like stdin)
    Where can I find it? I can't find any references to it in the documentation.

    Amir
  • Frans Englich

    #2
    Re: A completely silly question

    On Friday 17 December 2004 16:40, Amir Dekel wrote:[color=blue]
    > This must be the silliest question ever:
    >
    > What about user input in Python? (like stdin)
    > Where can I find it? I can't find any references to it in the
    > documentation.[/color]

    See sys.stdin


    Cheers,

    Frans

    Comment

    • Harlin Seritt

      #3
      Re: A completely silly question

      Amir Dekel wrote:
      [color=blue]
      > This must be the silliest question ever:
      >
      > What about user input in Python? (like stdin)
      > Where can I find it? I can't find any references to it in the
      > documentation.
      >
      > Amir[/color]

      Simple, Simple, Simple:

      Var = raw_input("Some prompting text here: ")


      --
      Harlin Seritt

      Comment

      • Amir Dekel

        #4
        Re: A completely silly question

        Harlin Seritt wrote:
        [color=blue]
        >
        > Simple, Simple, Simple:
        >
        > Var = raw_input("Some prompting text here: ")
        >[/color]

        Frans Englich wrote:[color=blue]
        >
        > See sys.stdin
        >[/color]

        What I need from the program is to wait for a single character input,
        something like while(getchar() ) in C. All those Python modules don't
        make much sence to me...

        Amir

        Comment

        • Steven Bethard

          #5
          Re: A completely silly question

          Amir Dekel wrote:[color=blue]
          > What I need from the program is to wait for a single character input,
          > something like while(getchar() ) in C. All those Python modules don't
          > make much sence to me...[/color]

          sys.stdin.read( 1)

          but if you're having trouble reading the module documentation, maybe you
          could elaborate on what's giving you trouble. The answer to 99% of
          questions on this list is in the documentation somewhere, so if you
          don't know how to read it, you're going to have trouble with Python.

          Steve

          Comment

          • Peter Hansen

            #6
            Re: A completely silly question

            Amir Dekel wrote:[color=blue]
            > What I need from the program is to wait for a single character input,
            > something like while(getchar() ) in C.[/color]

            Try the "msvcrt" module if you are on Windows.

            If you are not, remember to specify your platform next time
            you ask a question...
            [color=blue]
            > All those Python modules don't
            > make much sence to me...[/color]

            That's going to make it hard to program in Python, I suspect.
            Maybe it would be helpful to run through the tutorial, or
            spend more time reading the docs.

            -Peter

            Comment

            • Mike Meyer

              #7
              Re: A completely silly question

              Steven Bethard <steven.bethard @gmail.com> writes:
              [color=blue]
              > Amir Dekel wrote:[color=green]
              >> What I need from the program is to wait for a single character
              >> input, something like while(getchar() ) in C. All those Python
              >> modules don't make much sence to me...[/color]
              >
              > sys.stdin.read( 1)[/color]

              That doesn't do what he wants, because it doesn't return until you hit
              a newline.

              The answer is system dependent. Or you can use massive overkill and
              get curses, but if you're on windows you'll have to use a third party
              curses package, and maybe wrap it


              --
              Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
              Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

              Comment

              • wes weston

                #8
                Re: A completely silly question

                Amir Dekel wrote:[color=blue]
                > Harlin Seritt wrote:
                >[color=green]
                >>
                >> Simple, Simple, Simple:
                >>
                >> Var = raw_input("Some prompting text here: ")
                >>[/color]
                >
                > Frans Englich wrote:[color=green]
                > >
                > > See sys.stdin
                > >[/color]
                >
                > What I need from the program is to wait for a single character input,
                > something like while(getchar() ) in C. All those Python modules don't
                > make much sence to me...
                >
                > Amir[/color]

                Amir,
                [color=blue][color=green][color=darkred]
                >>> import tkSimpleDialog
                >>> ch = tkSimpleDialog. askstring("","c h?")[/color][/color][/color]

                wes

                Comment

                • marco

                  #9
                  Re: A completely silly question

                  Amir Dekel wrote:
                  [color=blue]
                  >
                  > What I need from the program is to wait for a single character input,
                  > something like while(getchar() ) in C. All those Python modules don't
                  > make much sence to me...[/color]

                  Take a look at Alan Gauld's "Learning to Program"
                  (http://www.freenetpages.co.uk/hp/alan.gauld/)
                  in the section "Event Driven programming"

                  Hope it helps

                  Marco

                  Comment

                  • Steven Bethard

                    #10
                    Re: A completely silly question

                    Mike Meyer wrote:[color=blue]
                    > That doesn't do what he wants, because it doesn't return until you hit
                    > a newline.[/color]

                    Are you sure that's not just an artifact of how your terminal buffers
                    data for sys.stdin?

                    $ cat temp.py
                    import sys
                    char = sys.stdin.read( 1)
                    while char:
                    print char
                    char = sys.stdin.read( 1)
                    $ cat temp.txt
                    abc
                    def
                    $ python temp.py < temp.txt
                    a
                    b
                    c


                    d
                    e
                    f

                    Of course if the intent is to have this work with terminal input, then
                    yes, sys.stdin.read( 1) is probably not going to do the right thing...

                    Steve

                    Comment

                    • Mike Meyer

                      #11
                      Re: A completely silly question

                      Steven Bethard <steven.bethard @gmail.com> writes:
                      [color=blue]
                      > Mike Meyer wrote:[color=green]
                      >> That doesn't do what he wants, because it doesn't return until you hit
                      >> a newline.[/color]
                      > Of course if the intent is to have this work with terminal input, then
                      > yes, sys.stdin.read( 1) is probably not going to do the right thing...[/color]

                      That's what the OP asked for - a way to wait for a single character,
                      like while(getchar() ) in C.

                      Hmm. That tells me he's probably on a Windows box, so my unix solution
                      wouldn't do him much good.

                      <mike
                      --
                      Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                      Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                      Comment

                      • Amir Dekel

                        #12
                        Re: A completely silly question

                        Mike Meyer wrote:
                        [color=blue]
                        >
                        > Hmm. That tells me he's probably on a Windows box, so my unix solution
                        > wouldn't do him much good.
                        >[/color]
                        Yes, Windows...too bad

                        Comment

                        • David Bolen

                          #13
                          Re: A completely silly question

                          Mike Meyer <mwm@mired.or g> writes:
                          [color=blue]
                          > Steven Bethard <steven.bethard @gmail.com> writes:
                          >[color=green]
                          > > Amir Dekel wrote:[color=darkred]
                          > >> What I need from the program is to wait for a single character
                          > >> input, something like while(getchar() ) in C. All those Python
                          > >> modules don't make much sence to me...[/color]
                          > >
                          > > sys.stdin.read( 1)[/color]
                          >
                          > That doesn't do what he wants, because it doesn't return until you hit
                          > a newline.[/color]

                          Well, but that's true as well for getchar() (at least in many cases of
                          interactive input and line buffering), so in that respect I do think
                          it's a fairly direct replacement, depending on how the OP was going to
                          use getchar() in the application.

                          For example, compare: with:

                          #include <stdio.h> >>> import sys[color=blue][color=green][color=darkred]
                          >>> while 1:[/color][/color][/color]
                          main() ... c = sys.stdin.read( 1)
                          { ... print ord(c),
                          while (1) { ...
                          int ch = getchar();
                          printf("%d ",ch);
                          }
                          }

                          When run, both produce (at least for me):

                          0123456789 (hit Enter here)
                          48 49 50 51 52 53 54 55 56 57 10

                          under both Unix (at least FreeBSD/Linux in my quick tests) and Windows
                          (whether MSVC or Cygwin/gcc).

                          (I don't include any output buffer flushing, since it shouldn't be
                          needed on an interactive terminal, but you could add that to ensure
                          that it isn't the output part that is being buffered - I did try it
                          just to be sure on the Unix side)
                          [color=blue]
                          > The answer is system dependent. Or you can use massive overkill and
                          > get curses, but if you're on windows you'll have to use a third party
                          > curses package, and maybe wrap it[/color]

                          If you want to guarantee you'll get the next console character without
                          any waiting under Windows there's an msvcrt module that contains
                          functions like kbhit() and getch[e] that would probably serve.

                          -- David

                          Comment

                          • Mike Meyer

                            #14
                            Re: A completely silly question

                            David Bolen <db3l@fitlinxx. com> writes:
                            [color=blue]
                            > Mike Meyer <mwm@mired.or g> writes:
                            >[color=green]
                            >> Steven Bethard <steven.bethard @gmail.com> writes:
                            >>[color=darkred]
                            >> > Amir Dekel wrote:
                            >> >> What I need from the program is to wait for a single character
                            >> >> input, something like while(getchar() ) in C. All those Python
                            >> >> modules don't make much sence to me...
                            >> >
                            >> > sys.stdin.read( 1)[/color]
                            >>
                            >> That doesn't do what he wants, because it doesn't return until you hit
                            >> a newline.[/color]
                            >
                            > Well, but that's true as well for getchar() (at least in many cases of
                            > interactive input and line buffering), so in that respect I do think
                            > it's a fairly direct replacement, depending on how the OP was going to
                            > use getchar() in the application.[/color]

                            The OP said "wait for a single character input". sys.stdin.read( 1)
                            waits for a newline.

                            <mike
                            --
                            Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                            Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                            Comment

                            • Fredrik Lundh

                              #15
                              Re: A completely silly question

                              >> Well, but that's true as well for getchar() (at least in many cases of[color=blue][color=green]
                              >> interactive input and line buffering), so in that respect I do think
                              >> it's a fairly direct replacement, depending on how the OP was going to
                              >> use getchar() in the application.[/color]
                              >
                              > The OP said "wait for a single character input". sys.stdin.read( 1)
                              > waits for a newline.[/color]

                              in the same same sentence, the OP also said that he wanted something like
                              C's getchar(). if you guys are going to read only parts of the original post,
                              you could at least try to read an entire sentence, before you start arguing...

                              </F>



                              Comment

                              Working...