469,645 Members | 1,702 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

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.

Jul 21 '05 #1
8 1697
Use raw_input instead. It returns a string of whatever was typed. Input
expects a valid python expression.

Jul 21 '05 #2
Devan L wrote:
Use raw_input instead. It returns a string of whatever was typed. Input
expects a valid python expression.


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
Jul 21 '05 #3
On Thursday 14 July 2005 07:00 am, Michael Hoffman wrote:
Devan L wrote:
Use raw_input instead. It returns a string of whatever was typed. Input
expects a valid python expression.


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.


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 anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 21 '05 #4
On 15/07/05, Terry Hancock <ha*****@anansispaceworks.com> wrote:
On Thursday 14 July 2005 07:00 am, Michael Hoffman wrote:
Devan L wrote:
Use raw_input instead. It returns a string of whatever was typed. Input
expects a valid python expression.


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.


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).


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
Jul 21 '05 #5
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" <st************@gmail.com>
To: <ha*****@anansispaceworks.com>
Cc: <py*********@python.org>
Sent: Sunday, July 17, 2005 11:12 PM
Subject: Re: Who uses input()? [was Re: question on "input"]

On 15/07/05, Terry Hancock <ha*****@anansispaceworks.com> wrote:
On Thursday 14 July 2005 07:00 am, Michael Hoffman wrote:
> Devan L wrote:
> > Use raw_input instead. It returns a string of whatever was typed. Input > > expects a valid python expression.
>
> 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.


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).


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

Jul 21 '05 #6

"Nathan Pinno" <fa********@hotmail.com> wrote in message
news:BA**********************************@phx.gbl. ..
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,


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

Jul 21 '05 #7
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!!!
>>>

++++++++++++++++++++++++++++++++++++++++++++
Sep 28 '05 #8
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
Sep 29 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Pete Wilson | last post: by
5 posts views Thread by John Oliver | last post: by
8 posts views Thread by Ulysse | last post: by
2 posts views Thread by Angus | last post: by
5 posts views Thread by Maria Sudderman | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.