[Tutor] User input question

Rob rob@uselesspython.com
Thu, 8 Aug 2002 10:57:36 -0500


I decided to follow up on myself and show an example of unexpected
side-effects of the use of input():

>>> def silliness():
	for i in range(1,4):
		print i


>>> myInt = input("Please input an integer: ")
Please input an integer: silliness()
1
2
3

In this case, the programmer had a function called silliness(), which has
nothing to do with the request for an integer to be input from the user. The
user input a call to the silliness() function instead of an integer, and
input() did precisely what the programmer told it to do, which turned out to
be an invocation of a function.

In this case, the result was fairly harmless. However, you might be able to
imagine some examples in which the result would be less pleasant.

This isn't intended to scare people off from the use of input(), of course.
As you can see, it is a powerful tool indeed, and you just might find it
comes in handy later on.

Rob
http://uselesspython.com

> -----Original Message-----
> From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
> Rob
> Sent: Thursday, August 08, 2002 10:27 AM
> To: Python Tutor
> Subject: RE: [Tutor] User input question
>
>
> May I also add a suggestion about the use of input() in this
> case? When you
> use input(), a user can provide input that you might expect, which could
> cause problems.
>
> An alternative solution is to use raw_input, and convert the input to the
> desired integer:
>
> >>> myInt = raw_input('try a number ')
> try a number 4
> >>> myInt
> '4'
>
> # notice that '4' is a string
>
> >>> myInt = int(myInt)
>
> # this converts myInt into an integer and stores it under the same name
>
> >>> myInt
> 4
>
> # myInt is now an integer!
>
> Rob
> http://uselesspython.com
>
> > -----Original Message-----
> > From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
> > David Turner
> > Sent: Thursday, August 08, 2002 9:57 AM
> > To: tutor@python.org
> > Subject: RE: [Tutor] User input question
> >
> >
> > OK guys, bear with me - this is my first EVER foray into Python,
> > but I think
> > I may have stumbled clumsily into a solution (of sorts)....
> >
> > Here goes....
> >
> > *****
> >
> > def timestab(n):
> >     m = 1
> >     if i < 13:
> >                while m < 14:
> >                        print "%d x %d = %d" % (m,n,m*n)
> >                        m = m + 1
> >     else:
> >                print "Only positive numbers between 1 and 12 please!"
> >
> >
> > i = input('Try a number ')
> > print timestab(i)
> >
> > *****
> >
> >
> > This seems to work OK (but I guess there will be far cleaner,
> > neater ways to
> > do it), and it doesn't get rid of the "None" at the bottom of
> the list...
> >
> > But anyway, since this was my first ever go with Python, I'm
> quite pleased
> > with myself nonetheless!!  *LARGE GRIN*
> >
> > Cheers
> >
> > DT
> >
> > -----Original Message-----
> > From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
> > shey crompton
> > Sent: 08 August 2002 15:32
> > To: tutor
> > Subject: RE: [Tutor] User input question
> >
> >
> > That's got rid of the error message, thanks.
> > It now just squares the number that the user inputs, and also
> > returns 'None'
> > on the line below (confused?).
> >
> > I am thinking I need a range command to get it to do multiply within a
> > range. I have just tried adding:
> > 	For n in range(1,13):
> > Between the 'def' line and the if statement with no result. Oh
> well, gives
> > me something else to ponder on during an otherwise boring day
> at work. :-)
> >
> >
> >  -----Original Message-----
> > From: 	Kyle Babich [mailto:kb@mm.st]
> > Sent:	08 August 2002 15:02
> > To:	shey crompton; tutor
> > Subject:	Re: [Tutor] User input question
> >
> > I'm still a newbie to python so I am taking my best guess in saying to
> > un-indent the print that is causing the error.
> >
> > On Thu, 8 Aug 2002 14:15:32 +0100, "shey crompton" <shey@argonaut.com>
> > said:
> > > I have been trying to modify a script that prints out the times tables
> > > to
> > > one that asks the user to input which times table they would like
> > > printed
> > > out (between 1 and 13).
> > > I have tried so many different ways of doing this without any success.
> > > I am
> > > sure it's a simple answer, but I am starting to get frustrated.
> > > The script below is my latest attempt, and the error message I get is
> > > below
> > > it.
> > > Hints, and tips greatly appreciated.
> > >
> > > Shey
> > >
> > >
> > > def timestab(n):
> > >     if i < 13:
> > >         print "%d x %d = %d" % (i,n,i*n)
> > >     else:
> > >         print "Only positive numbers between 1 and 12 please!"
> > >
> > > i = input('Try a number ')
> > >     print timestab(i)
> > >
> > >
> > > The error message is:
> > >
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>