Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

question about input() and/or raw_input()

53 views
Skip to first unread message

Roy Smith

unread,
Jan 18, 2014, 1:30:20 PM1/18/14
to
Pardon me for being cynical, but in the entire history of the universe,
has anybody ever used input()/raw_input() for anything other than a
homework problem?

Mark Lawrence

unread,
Jan 18, 2014, 1:41:41 PM1/18/14
to pytho...@python.org
Not me personally. I guess raw_input must have been used somewhere at
some time for something, or it would have been scrapped in Python 3, not
renamed to input.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Emile van Sebille

unread,
Jan 18, 2014, 1:49:19 PM1/18/14
to pytho...@python.org
Yes - routinely.

Emile



Peter Otten

unread,
Jan 18, 2014, 2:05:57 PM1/18/14
to pytho...@python.org
I use it for pointless throwaway tools, sometimes via the cmd module,
sometimes directly.

I like that you can add tab-completion.

Terry Reedy

unread,
Jan 18, 2014, 4:33:18 PM1/18/14
to pytho...@python.org
Homework problems (and 'toy' programs, such as hangman), whether in a
programming class or elsewhere, are one of the intended use cases of
Python. How else would you get interactive input without the complexity
of a gui?

--
Terry Jan Reedy

Message has been deleted

Chris Angelico

unread,
Jan 18, 2014, 9:46:27 PM1/18/14
to pytho...@python.org
On Sun, Jan 19, 2014 at 8:33 AM, Terry Reedy <tjr...@udel.edu> wrote:
> On 1/18/2014 1:30 PM, Roy Smith wrote:
>>
> Homework problems (and 'toy' programs, such as hangman), whether in a
> programming class or elsewhere, are one of the intended use cases of Python.
> How else would you get interactive input without the complexity of a gui?

With the network :) I've written plenty of programs whose sole
interaction is via sockets (telnet, HTTP, SMTP, whatever), or a
database, or somesuch.

But I've also written my share of interactive programs that use the
console. Plenty of programs don't need the fanciness of a GUI, but
need to prompt the user for stuff. If I write something for my brother
(and only him), I'm inclined to spend less effort on the UI than I
would for something of wide distribution, and console I/O is
approximately zero effort.

BTW, I'm assuming your mention of "input()/raw_input()" is covering
Py3 and Py2, respectively. I have *never* used input() in live Py2
code, and never intend to. It's way too subtle. On those really rare
occasions when you actually want to take something from the user and
immediately eval it, the extra keystrokes for eval(raw_input()) are,
IMO, a small price for the clarity.

ChrisA

Rustom Mody

unread,
Jan 18, 2014, 11:15:25 PM1/18/14
to
Similar 'cynicism' regarding print would be salutary for producing better programmers

[If youve taught programming and had to deal with code strewn with prints...]

Chris Angelico

unread,
Jan 18, 2014, 11:21:36 PM1/18/14
to pytho...@python.org
Why, exactly? How ought a program to produce filterable output?

ChrisA

Rustom Mody

unread,
Jan 18, 2014, 11:43:39 PM1/18/14
to
On Sunday, January 19, 2014 9:51:36 AM UTC+5:30, Chris Angelico wrote:
> On Sun, Jan 19, 2014 at 3:15 PM, Rustom Mody wrote:
> > On Sunday, January 19, 2014 12:00:20 AM UTC+5:30, Roy Smith wrote:
> >> Pardon me for being cynical, but in the entire history of the universe,
> >> has anybody ever used input()/raw_input() for anything other than a
> >> homework problem?
> > Similar 'cynicism' regarding print would be salutary for producing better programmers
> > [If youve taught programming and had to deal with code strewn with prints...]

> Why, exactly? How ought a program to produce filterable output?

Because these two pieces of code

>>> def foo(x): print x+1

>>> def bar(x): return x+1

look identical (to a beginner at least)

>>> foo(3)
4
>>> bar(3)
4
>>>

And so if they see prints used cavalierly for demo purposes, they think the
prints are also ok for production.

As a professional programmer, you would of course understand
- 'normal' code that does some processing and then some output should not
have prints in the processing
- web-serving (type of) code that has little other than heavy-duty printing
should probably use a template engine of some sort

In any case prints all over is a code-smell
exacerbated by the way that manuals/examples need to be written

Chris Angelico

unread,
Jan 18, 2014, 11:59:58 PM1/18/14
to pytho...@python.org
On Sun, Jan 19, 2014 at 3:43 PM, Rustom Mody <rusto...@gmail.com> wrote:
> Because these two pieces of code
>
>>>> def foo(x): print x+1
>
>>>> def bar(x): return x+1
>
> look identical (to a beginner at least)
>
>>>> foo(3)
> 4
>>>> bar(3)
> 4
>>>>

As do these pieces of code:

>>> def quux(x): return str(x+1)
>>> def quux(x): return hex(x+1)[2:]

But we don't decry hex or str because of it. Every function has its
use and purpose. If someone uses the wrong tool for the job, s/he will
have to figure that out at some point - it doesn't mean the tool is
wrong.

If you're not using the REPL, print is critical. Don't assume everyone
uses interactive mode.

ChrisA

Steven D'Aprano

unread,
Jan 19, 2014, 1:24:24 AM1/19/14
to
Yes. They are excellent for interactive command line tools.


--
Steven

Rustom Mody

unread,
Jan 19, 2014, 3:26:34 AM1/19/14
to
On Sunday, January 19, 2014 10:29:58 AM UTC+5:30, Chris Angelico wrote:
> On Sun, Jan 19, 2014 at 3:43 PM, Rustom Mody wrote:
> > Because these two pieces of code
> >>>> def foo(x): print x+1
> >>>> def bar(x): return x+1
> > look identical (to a beginner at least)
> >>>> foo(3)
> > 4
> >>>> bar(3)
> > 4

> As do these pieces of code:

> >>> def quux1(x): return str(x+1)
> >>> def quux2(x): return hex(x+1)[2:]

They do?

>>> quux1(2.3)
'3.3'

>>> quux2(2.3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in quux2
TypeError: hex() argument can't be converted to hex


If you want to give an irrelevant example at least give a correct one :D
the difference between str and hex is an arcane difference (Ive never used hex)
the difference between functions and procedures is absolutely basic.

And python is worse than not just academic languages like haskell in this
respect but even worse than C/Pascal etc.

In Pascal, the difference between procedure and function is
fundamental to the lang and is key to Pascal being good for academic
purposes.

In C, the difference is not so well marked but at least trivial
code-examples found in books/the net wont run straight-off without
some main-printf-etc boiler-plate.

In python lots of easy to check examples run straight off --
convenient for programmers of course but a headache for teachers who
are trying to set habits of minimal hygiene

> But we don't decry hex or str because of it. Every function has its
> use and purpose. If someone uses the wrong tool for the job, s/he will
> have to figure that out at some point - it doesn't mean the tool is
> wrong.

> If you're not using the REPL, print is critical. Don't assume everyone
> uses interactive mode.

"Everyone uses interactive mode" is of course an unreasonable assumption
"Everyone needs to learn (something or other at some time or other)" is not

And print (especially in python) screws up the learning-curve

tl;dr
You are wearing 'professional programmer' hat
I am wearing 'teacher' hat
Not sure what hat Roy or Steven are wearing

Chris Angelico

unread,
Jan 19, 2014, 5:39:50 AM1/19/14
to pytho...@python.org
On Sun, Jan 19, 2014 at 7:26 PM, Rustom Mody <rusto...@gmail.com> wrote:
> If you want to give an irrelevant example at least give a correct one :D
> the difference between str and hex is an arcane difference (Ive never used hex)
> the difference between functions and procedures is absolutely basic.

They don't give the same result for every possible input, any more
than your two do:

>>> foo(1.234567890123456)
2.23456789012
>>> bar(1.234567890123456)
2.234567890123456

(Tested on 2.7.3 on Linux. YMMV.)

There's no difference in Python between functions and procedures. It's
all functions, and some of them implicitly return None. If there were
a difference, what would this be?

def none_if(value, predicate):
if not predicate(value): return value

Personally, I'm quite happy with Python and the print function. (Or
statement, if you prefer. Same difference.) The most fundamental
aspects of any program are input, output, and preferably some
computation in between; and the most fundamental forms of input are
the command line / console and the program's source, and the most
basic output is the console. So the most basic and obvious program
needs:

1) Access to the command-line arguments
2) The ability to read from the console
3) Some means of writing to the console.

Not every program will need all that, but they'd be the most obvious
and simplest methods of communication - especially since they're the
three that are easiest to automate. How do you run a GUI program
through automated testing? With difficulty. How do you run a stdio
program through automated testing? Pipe it some input and compare its
output to the standard. And that means people should be accustomed to
using print, and sys.argv, and (raw_)input.

Fortunately Python doesn't have ob_start() / ob_get_clean() to tempt
people to use print when they should use return. So there's no
problem.

ChrisA

Grant Edwards

unread,
Jan 19, 2014, 11:14:48 AM1/19/14
to
sys.stdin.readline()

Ethan Furman

unread,
Jan 19, 2014, 11:14:22 AM1/19/14
to pytho...@python.org
On 01/19/2014 12:26 AM, Rustom Mody wrote:
> On Sunday, January 19, 2014 10:29:58 AM UTC+5:30, Chris Angelico wrote:
>> On Sun, Jan 19, 2014 at 3:43 PM, Rustom Mody wrote:
>>>
>> As do these pieces of code:
>>
>>--> def quux1(x): return str(x+1)
>>--> def quux2(x): return hex(x+1)[2:]
>
> They do?
>
>--> quux1(2.3)
> '3.3'
>
>--> quux2(2.3)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 1, in quux2
> TypeError: hex() argument can't be converted to hex

(Will be) fixed in 3.5 [1] :)

--
~Ethan~

[1] Which is to say, both will raise an exception.

Chris Angelico

unread,
Jan 19, 2014, 11:38:29 AM1/19/14
to pytho...@python.org
On Mon, Jan 20, 2014 at 3:14 AM, Ethan Furman <et...@stoneleaf.us> wrote:
>>> --> def quux1(x): return str(x+1)
>> --> quux1(2.3)
>> '3.3'
>
> (Will be) fixed in 3.5 [1] :)
> [1] Which is to say, both will raise an exception.

Why would that raise?

ChrisA
Message has been deleted

Grant Edwards

unread,
Jan 19, 2014, 12:42:55 PM1/19/14
to
On 2014-01-19, Dennis Lee Bieber <wlf...@ix.netcom.com> wrote:
> On Sun, 19 Jan 2014 16:14:48 +0000 (UTC), Grant Edwards
><inv...@invalid.invalid> declaimed the following:
> At which point a simple:
>
> nm = raw_input("Enter your name: ")
> print "Hello, ", nm
>
> turns into (to duplicate the behavior WRT line endings, and to minimize the
> new features the student is exposed to)
>
> import sys
>
> sys.stdout.write("Enter your name: ")
> nm = sys.stdin.readline()
> nm.strip()
> sys.stdout.write("Hello, ")
> sys.stdout.write(nm)
> sys.stdout.write("\n")
> sys.stdout.flush()

The question was how to get input without using a GUI. I presented an
answer. You then conflated "whether or not to use input" with
"whether or not to use print" and proceeded to construct and knock
down a very nice straw man. Kudos.

import sys
print "Enter your name: ",
nm = sys.stdin.readline().strip()
print "Hello, ", nm

> Yes, a non-beginner would have been exposed to formatting operations
> and been able to condense the three .write() calls into one...

1) I don't get why you jumped on the whole print vs. string formatting
thing. We're not talking about print vs write. We're talking
about whether or not people use input and raw_input. I've been
writing Python programs for 15 years, and I've never used either
input or raw_input.

2) I didn't claim that sys.stdin.readline() was as simple as using
input. I didn't claim it was preferable. I merely presented it as
a refutation to the argument that if you don't use input/raw_input
then you have to use a GUI toolkit.

--
Grant



Chris Angelico

unread,
Jan 19, 2014, 12:59:53 PM1/19/14
to pytho...@python.org
On Mon, Jan 20, 2014 at 4:42 AM, Grant Edwards <inv...@invalid.invalid> wrote:
> 2) I didn't claim that sys.stdin.readline() was as simple as using
> input. I didn't claim it was preferable. I merely presented it as
> a refutation to the argument that if you don't use input/raw_input
> then you have to use a GUI toolkit.

I'd draw a subtle distinction here, btw. With sys.stdin.readline(),
you're asking to read a line from standard input, but with
(raw_)input(), you're asking to read one line from the console. If
it's been redirected, that's going to be equivalent (modulo newline
handling), but if not, it would make sense for (raw_)input() to call
on GNU Readline, where sys.stdin.readline() shouldn't. Calling a
method on a file-like object representing stdin feels lower level than
"ask the user for input with this prompt" (which might well do more
than that, too - eg it might flush sys.stdout).

ChrisA

Mark Lawrence

unread,
Jan 19, 2014, 1:07:44 PM1/19/14
to pytho...@python.org
On 18/01/2014 18:41, Mark Lawrence wrote:
> On 18/01/2014 18:30, Roy Smith wrote:
> Not me personally. I guess raw_input must have been used somewhere at
> some time for something, or it would have been scrapped in Python 3, not
> renamed to input.
>

Actually, to go off at a tangent, I'm just getting into GUIs via
wxPython. I've discovered there are distinct advantages having to write
endless lines of code just to get a piece of data. For example on a
Sunday it helps pass the time between the two sessions of the Masters
Snooker final being shown on TV.

Grant Edwards

unread,
Jan 19, 2014, 1:15:40 PM1/19/14
to
On 2014-01-19, Mark Lawrence <bream...@yahoo.co.uk> wrote:
> On 18/01/2014 18:41, Mark Lawrence wrote:
>> On 18/01/2014 18:30, Roy Smith wrote:
>>> Pardon me for being cynical, but in the entire history of the universe,
>>> has anybody ever used input()/raw_input() for anything other than a
>>> homework problem?
>>
>> Not me personally. I guess raw_input must have been used somewhere at
>> some time for something, or it would have been scrapped in Python 3, not
>> renamed to input.
>
> Actually, to go off at a tangent, I'm just getting into GUIs via
> wxPython. I've discovered there are distinct advantages having to
> write endless lines of code just to get a piece of data. For example
> on a Sunday it helps pass the time between the two sessions of the
> Masters Snooker final being shown on TV.

Fair enough, but what do you do to pass the time _during_ Snooker
being shown on TV?

I can still remember the point in my first trip to the UK when I
accidentally stumbled across darts on TV. Given the endless variety
(and quantity) of pointless crap that people watch here in the US, I
can't really explain why I was so baffled and amused by darts on TV --
but I was.

--
Grant Edwards grant.b.edwards Yow! I want EARS! I want
at two ROUND BLACK EARS
gmail.com to make me feel warm
'n secure!!

Ethan Furman

unread,
Jan 19, 2014, 12:50:08 PM1/19/14
to pytho...@python.org
Sorry, should have read that closer. It will not raise.

The difference I was thinking of is:

"%h" % 3.14 # this works

vs.

hex(3.14) # this raises

In 3.5 both will raise.

Apologies for the noise.

--
~Ethan~

Roy Smith

unread,
Jan 19, 2014, 1:37:29 PM1/19/14
to
In article <lbh4oc$nqv$1...@reader1.panix.com>,
Grant Edwards <inv...@invalid.invalid> wrote:

> I can still remember the point in my first trip to the UK when I
> accidentally stumbled across darts on TV. Given the endless variety
> (and quantity) of pointless crap that people watch here in the US, I
> can't really explain why I was so baffled and amused by darts on TV --
> but I was.

What's so complicated?

points = 501
for dart in throws():
if points - dart == 0 and dart.is_double():
raise YouWin
if points - dart < 0:
continue
points -= dart
beer.drink()

Chris Angelico

unread,
Jan 19, 2014, 1:41:34 PM1/19/14
to pytho...@python.org
On Mon, Jan 20, 2014 at 4:50 AM, Ethan Furman <et...@stoneleaf.us> wrote:
> The difference I was thinking of is:
>
> "%h" % 3.14 # this works
>
> vs.
>
> hex(3.14) # this raises
>
> In 3.5 both will raise.

Now you have me *thoroughly* intrigued. It's not %h (incomplete format
- h is a modifier), nor %H (unsupported format character). Do you mean
%x? As of 3.4.0b2, that happily truncates a float:

>>> "%x" % 3.14
'3'

Is that changing in 3.5? Seems a relatively insignificant point, tbh!
Anyway, no biggie.

ChrisA

Chris Angelico

unread,
Jan 19, 2014, 1:43:08 PM1/19/14
to pytho...@python.org
On Mon, Jan 20, 2014 at 5:37 AM, Roy Smith <r...@panix.com> wrote:
> What's so complicated?
>
> points = 501
> for dart in throws():
> if points - dart == 0 and dart.is_double():
> raise YouWin
> if points - dart < 0:
> continue
> points -= dart
> beer.drink()

assert victory
raise beer

ChrisA

Grant Edwards

unread,
Jan 19, 2014, 2:11:14 PM1/19/14
to
That looks like an algorithm for _playing_ darts. That I understand.
I have two dartboards (one real, one electronic) and a coule decent
sets of darts. It's watching darts on TV that I don't get.

Actually, I don't really get watching any sort of sports on TV (even
the ones I play). But there was just something about darts on TV that
seemed particularly beyond the pale.

--
Grant Edwards grant.b.edwards Yow! But they went to MARS
at around 1953!!
gmail.com

Mark Lawrence

unread,
Jan 19, 2014, 2:17:30 PM1/19/14
to pytho...@python.org
Just no comparison, darts and snooker. This is excellent though
http://www.youtube.com/watch?v=sHnBppccI0o

Larry Martell

unread,
Jan 19, 2014, 2:24:52 PM1/19/14
to pytho...@python.org
On Sun, Jan 19, 2014 at 12:17 PM, Mark Lawrence <bream...@yahoo.co.uk> wrote:
> On 19/01/2014 18:15, Grant Edwards wrote:
>>
> Just no comparison, darts and snooker. This is excellent though
> http://www.youtube.com/watch?v=sHnBppccI0o

Now that we're way off on the tangent of what some people consider
boring and others don't, I'm really looking forward to watching
curling in the upcoming Olympics.

Mark Lawrence

unread,
Jan 19, 2014, 2:29:26 PM1/19/14
to pytho...@python.org
On 19/01/2014 19:24, Larry Martell wrote:
> On Sun, Jan 19, 2014 at 12:17 PM, Mark Lawrence <bream...@yahoo.co.uk> wrote:
>> On 19/01/2014 18:15, Grant Edwards wrote:
>>>
>> Just no comparison, darts and snooker. This is excellent though
>> http://www.youtube.com/watch?v=sHnBppccI0o
>
> Now that we're way off on the tangent of what some people consider
> boring and others don't, I'm really looking forward to watching
> curling in the upcoming Olympics.
>

Curling, now there's another good reason to allow Scottish independance :)

Ethan Furman

unread,
Jan 19, 2014, 2:16:12 PM1/19/14
to pytho...@python.org
Argh. Yes, %x or %X.

--
~Ethan~

Gene Heskett

unread,
Jan 19, 2014, 3:12:33 PM1/19/14
to pytho...@python.org
On Sunday 19 January 2014 15:11:52 Larry Martell did opine:

> On Sun, Jan 19, 2014 at 12:17 PM, Mark Lawrence <bream...@yahoo.co.uk>
wrote:
> > On 19/01/2014 18:15, Grant Edwards wrote:
> >> On 2014-01-19, Mark Lawrence <bream...@yahoo.co.uk> wrote:
> >>> Actually, to go off at a tangent, I'm just getting into GUIs via
> >>> wxPython. I've discovered there are distinct advantages having to
> >>> write endless lines of code just to get a piece of data. For
> >>> example on a Sunday it helps pass the time between the two sessions
> >>> of the Masters Snooker final being shown on TV.
> >>
> >> Fair enough, but what do you do to pass the time _during_ Snooker
> >> being shown on TV?
> >>
> >> I can still remember the point in my first trip to the UK when I
> >> accidentally stumbled across darts on TV. Given the endless variety
> >> (and quantity) of pointless crap that people watch here in the US, I
> >> can't really explain why I was so baffled and amused by darts on TV
> >> -- but I was.
> >
> > Just no comparison, darts and snooker. This is excellent though
> > http://www.youtube.com/watch?v=sHnBppccI0o
>
> Now that we're way off on the tangent of what some people consider
> boring and others don't, I'm really looking forward to watching
> curling in the upcoming Olympics.

I have Larry, and the suspense is not good for those with high blood
pressure.

Cheers, Gene
--
"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>
Required reading:
<http://culturalslagheap.wordpress.com/2014/01/12/elemental/>
I've enjoyed just about as much of this as I can stand.
A pen in the hand of this president is far more
dangerous than 200 million guns in the hands of
law-abiding citizens.

Gene Heskett

unread,
Jan 19, 2014, 3:09:22 PM1/19/14
to pytho...@python.org
On Sunday 19 January 2014 15:08:31 Roy Smith did opine:
Aren't you missing a fi there, or a next dart? ;-)

Cheers, Gene
--
"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>
Required reading:
<http://culturalslagheap.wordpress.com/2014/01/12/elemental/>
Baseball is a skilled game. It's America's game - it, and high taxes.
-- The Best of Will Rogers

Larry Martell

unread,
Jan 19, 2014, 3:22:03 PM1/19/14
to pytho...@python.org
On Sun, Jan 19, 2014 at 1:12 PM, Gene Heskett <ghes...@wdtv.com> wrote:
> On Sunday 19 January 2014 15:11:52 Larry Martell did opine:
>
>> On Sun, Jan 19, 2014 at 12:17 PM, Mark Lawrence <bream...@yahoo.co.uk>
> wrote:
>> > On 19/01/2014 18:15, Grant Edwards wrote:
>> >> On 2014-01-19, Mark Lawrence <bream...@yahoo.co.uk> wrote:
>> >>> Actually, to go off at a tangent, I'm just getting into GUIs via
>> >>> wxPython. I've discovered there are distinct advantages having to
>> >>> write endless lines of code just to get a piece of data. For
>> >>> example on a Sunday it helps pass the time between the two sessions
>> >>> of the Masters Snooker final being shown on TV.
>> >>
>> >> Fair enough, but what do you do to pass the time _during_ Snooker
>> >> being shown on TV?
>> >>
>> >> I can still remember the point in my first trip to the UK when I
>> >> accidentally stumbled across darts on TV. Given the endless variety
>> >> (and quantity) of pointless crap that people watch here in the US, I
>> >> can't really explain why I was so baffled and amused by darts on TV
>> >> -- but I was.
>> >
>> > Just no comparison, darts and snooker. This is excellent though
>> > http://www.youtube.com/watch?v=sHnBppccI0o
>>
>> Now that we're way off on the tangent of what some people consider
>> boring and others don't, I'm really looking forward to watching
>> curling in the upcoming Olympics.
>
> I have Larry, and the suspense is not good for those with high blood
> pressure.

Then don't watch the women:
http://oglympics.com/2010/02/16/women-of-curling-fire-on-ice/
0 new messages