[BangPypers] Basic of basic question

Anand Chitipothu anandology at gmail.com
Wed May 8 13:55:02 CEST 2013


In my 7+ years of Python programming I've never used the input/raw_input
functions. People coming from C background look for a scanf replacement in
Python, but that is now the way things are done in Python.

You just write a function and call it at the end of the script and run it.
Asking user for input is not very usually done in Python.

Anand



On Wed, May 8, 2013 at 5:12 PM, jitendra gupta <jitu.icfai at gmail.com> wrote:

> >>> x = input("Enter the nu\t")
> Enter the nu 3
> >>> type(x)
> <type 'int'>
> >>> x = input("Enter the STR\t")
> Enter the STR "3"
> >>> type(x)
> <type 'str'>
> >>>
>
> Use input(), this will take care of your data type
>
>
> On Tue, May 7, 2013 at 11:03 AM, Noufal Ibrahim <noufal at nibrahim.net.in
> >wrote:
>
> > Umesh Tiptur <umeshreloaded at yahoo.com> writes:
> >
> > > Hi,
> > >
> > > I am very new to programming in python. But I want to know is user
> > > hass inputted a number or a string
> > >
> > > Please help with this HOW to..
> >
> > Python doesn't have anything like C's scanf that can read input into
> > variables of fixed types.
> >
> > Anything that you read from the user (using raw_input[1]), will be
> > returned as a string. So even if the user types 2 and hits enter, you
> > will get back "2" (which is a string).
> >
> > To check whether this can be converted into a number, the usual way is
> > to try to use it like a number and catch the exception which will be
> > raised if the conversion fails. Here is a simple example.
> >
> > >>> x = "2"
> > >>> try:
> > ...   int(x)
> > ... except ValueError:
> > ...   print "Not a number"
> > ...
> > 2
> >
> >
> > >>> x = "abcd"
> > >>> try:
> > ...   int(x)
> > ... except ValueError:
> > ...   print "Not a number"
> > ...
> > Not a number
> >
> >
> > [...]
> >
> >
> > Footnotes:
> > [1]  http://docs.python.org/2/library/functions.html#raw_input
> >
> > --
> > Cordially,
> > Noufal
> > http://nibrahim.net.in
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > http://mail.python.org/mailman/listinfo/bangpypers
> >
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
Anand
http://anandology.com/


More information about the BangPypers mailing list