question on "input"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • b83503104@yahoo.com

    #1

    question on "input"

    Hi,

    I want to accept the user's answer yes or no.
    If I do this:

    answer = input('y or n?')

    and type y on the keyboard, python complains

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<string>", line 0, in ?
    NameError: name 'y' is not defined

    It seems like input only accepts numerals, or strings with quotes.
    Need solutions, thanks.

  • Devan L

    #2
    Re: question on &quot;input&quo t;

    Use raw_input instead. It returns a string of whatever was typed. Input
    expects a valid python expression.

    Comment

    • Michael Hoffman

      #3
      Who uses input()? [was Re: question on &quot;input&quo t;]

      Devan L wrote:[color=blue]
      > Use raw_input instead. It returns a string of whatever was typed. Input
      > expects a valid python expression.[/color]

      Who actually uses this? It's equivalent to eval(raw_input( prompt)) but
      causes a lot of newbie confusion. Python-dev archives revealed that
      someone tried to get this deprecated but Guido disagreed.
      --
      Michael Hoffman

      Comment

      • Terry Hancock

        #4
        Re: Who uses input()? [was Re: question on &quot;input&quo t;]

        On Thursday 14 July 2005 07:00 am, Michael Hoffman wrote:[color=blue]
        > Devan L wrote:[color=green]
        > > Use raw_input instead. It returns a string of whatever was typed. Input
        > > expects a valid python expression.[/color]
        >
        > Who actually uses this? It's equivalent to eval(raw_input( prompt)) but
        > causes a lot of newbie confusion. Python-dev archives revealed that
        > someone tried to get this deprecated but Guido disagreed.[/color]

        I don't think it should disappear, but it *does* seem more sensible for
        "raw_input" to be called "input" (or "readstring " or some such thing) and
        "input" to vanish into greater obscurity as "eval_input " or something.

        Unfortunately, that would break code if anything relied on "input", so I
        guess that would be a Py3K idea, and maybe the whole I/O concept
        will be rethought then (if the "print" statement is going to go away,
        anyway).

        --
        Terry Hancock ( hancock at anansispacework s.com )
        Anansi Spaceworks http://www.anansispaceworks.com

        Comment

        • Stephen Thorne

          #5
          Re: Who uses input()? [was Re: question on &quot;input&quo t;]

          On 15/07/05, Terry Hancock <hancock@anansi spaceworks.com> wrote:[color=blue]
          > On Thursday 14 July 2005 07:00 am, Michael Hoffman wrote:[color=green]
          > > Devan L wrote:[color=darkred]
          > > > Use raw_input instead. It returns a string of whatever was typed. Input
          > > > expects a valid python expression.[/color]
          > >
          > > Who actually uses this? It's equivalent to eval(raw_input( prompt)) but
          > > causes a lot of newbie confusion. Python-dev archives revealed that
          > > someone tried to get this deprecated but Guido disagreed.[/color]
          >
          > I don't think it should disappear, but it *does* seem more sensible for
          > "raw_input" to be called "input" (or "readstring " or some such thing) and
          > "input" to vanish into greater obscurity as "eval_input " or something.
          >
          > Unfortunately, that would break code if anything relied on "input", so I
          > guess that would be a Py3K idea, and maybe the whole I/O concept
          > will be rethought then (if the "print" statement is going to go away,
          > anyway).[/color]

          I don't see as "break input() using code" -> "not until py3k" as a
          logical cause/effect. No one should be using input() anyway, the only
          place it's at-all appropriate is in a python tutorial, with the 'guess
          the number' game.

          --
          Stephen Thorne
          Development Engineer

          Comment

          • Nathan Pinno

            #6
            Re: Who uses input()? [was Re: question on &quot;input&quo t;]

            I use input() all the time. I know many people say it ain't safe, but
            whose going to use it to crash their own comp? Only an insane person would,
            or a criminal trying to cover his/her tracks.

            Sorry if I waded into the debate, but this debate originated from one of
            my posts.

            Nathan Pinno
            ----- Original Message -----
            From: "Stephen Thorne" <stephen.thorne @gmail.com>
            To: <hancock@anansi spaceworks.com>
            Cc: <python-list@python.org >
            Sent: Sunday, July 17, 2005 11:12 PM
            Subject: Re: Who uses input()? [was Re: question on "input"]

            [color=blue]
            > On 15/07/05, Terry Hancock <hancock@anansi spaceworks.com> wrote:[color=green]
            >> On Thursday 14 July 2005 07:00 am, Michael Hoffman wrote:[color=darkred]
            >> > Devan L wrote:
            >> > > Use raw_input instead. It returns a string of whatever was typed.[/color][/color][/color]
            Input[color=blue][color=green][color=darkred]
            >> > > expects a valid python expression.
            >> >
            >> > Who actually uses this? It's equivalent to eval(raw_input( prompt))[/color][/color][/color]
            but[color=blue][color=green][color=darkred]
            >> > causes a lot of newbie confusion. Python-dev archives revealed that
            >> > someone tried to get this deprecated but Guido disagreed.[/color]
            >>
            >> I don't think it should disappear, but it *does* seem more sensible for
            >> "raw_input" to be called "input" (or "readstring " or some such thing)[/color][/color]
            and[color=blue][color=green]
            >> "input" to vanish into greater obscurity as "eval_input " or something.
            >>
            >> Unfortunately, that would break code if anything relied on "input", so[/color][/color]
            I[color=blue][color=green]
            >> guess that would be a Py3K idea, and maybe the whole I/O concept
            >> will be rethought then (if the "print" statement is going to go away,
            >> anyway).[/color]
            >
            > I don't see as "break input() using code" -> "not until py3k" as a
            > logical cause/effect. No one should be using input() anyway, the only
            > place it's at-all appropriate is in a python tutorial, with the 'guess
            > the number' game.
            >
            > --
            > Stephen Thorne
            > Development Engineer
            > --
            > http://mail.python.org/mailman/listinfo/python-list
            >[/color]

            Comment

            • Terry Reedy

              #7
              Re: Who uses input()? [was Re: question on &quot;input&quo t;]


              "Nathan Pinno" <falcon3166@hot mail.com> wrote in message
              news:BAY106-DAV7A5CF5B26D33 618DCED9AC4D50@ phx.gbl...[color=blue]
              > I use input() all the time. I know many people say it ain't safe, but
              > whose going to use it to crash their own comp? Only an insane person
              > would,[/color]

              This is usage Guido intended it for, not for production apps distributed to
              world.



              Comment

              • JerryKreps
                New Member
                • Sep 2005
                • 3

                #8
                Hi, folks --

                I'm a Python newbie. As you can see from the session copied below, I have the latest version of Python and I've been using the Editor-Shell of the latest version of Boa Constructor while going through some Python tutorials. Everything was working as expected until I started using the raw_input built-in function. There seems to be some unreliable behavior in the Boa Editor - Shell. The input that is entered has the first four bytes dropped before it is stored in the test_string. The problem is inconsistant. It sometimes works fine, then starts dropping the first four characters again. The Idle IDE shell does not exhibit this problem. Any ideas, oh Knights of Ni?? Thanks! :)

                -- Jerry Kreps

                +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++

                # Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
                # wxPython 2.6.1.0, Boa Constructor 0.4.4
                # Type "copyright" , "credits" or "license" for more information.


                >>> test_string_01 = raw_input("What is your name?")
                What is your name?
                <<<
                Sir Lancelot
                >>> print test_string_01
                Lancelot


                >>> test_string_02 = raw_input("What is your quest? ")
                What is your quest?
                <<<
                To seek the grail
                >>> print test_string_02
                eek the grail


                >>> test_string_03 = raw_input("What is your favorite color?")
                What is your favorite color?
                <<<
                Orange
                >>> print test_string_03
                ge


                >>> test_string_04 = raw_input("Why are the first four characters dropped? ")
                Why are the first four characters dropped?
                <<<
                Run away!!!
                >>> print test_string_04
                away!!!
                >>>

                +++++++++++++++ +++++++++++++++ ++++++++++++++

                Comment

                • JerryKreps
                  New Member
                  • Sep 2005
                  • 3

                  #9
                  Hi, again.

                  Since the post before it was over two months ago, I am moving my previous post directly above to a new thread in hope that it will get more notice. I hope this does not break any forum rules.

                  -- Jerry Kreps :o

                  Comment

                  Working...