Sunday, March 4, 2012

Get Input from User


In this post we will see how do we take inputs from a user in Python

For this there are two ways, one input() and the other is raw_input()

input() is used when we want the user to input a integer type data like numbers, fractions..

Syntax: input("Integers type data goes here")

And raw_input() is used when we want the user to input a string type data like characters.

Syntax: raw_input("String type data goes here")

Lets see the difference with some examples:

As usual open up the IDLE

>>> a = input("Enter number here: ")
Enter number here: 8
>>> print "The value of variable \"a\" is: " + `a`
The value of variable "a" is: 8
>>>

[If you dint understand the print statement, please look here: Printing Strings In Python]

What we did here was, the variable a will store the value entered by the user, and then the print statement, displays the value of the variable a.

Now if try to enter a string, with input(), lets see what happens

>>> a = input("Input here: ")
Input here: subir

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    a = input("Input here: ")
  File "<string>", line 1, in <module>
NameError: name 'subir' is not defined
>>>

We see that it will accept a string type data with input().


Now lets see how do we let a user input string type data.


>>> a = raw_input("Enter name here: ")
Enter name here: subir
>>> print "Your name is: "+a
Your name is: subir
>>>


The raw_input allowed the user to input a string type data and assigned it to the variable "a"

Now what if we enter a integer type data with raw_input(). Lets check it out.

>>> a = raw_input("Input here: ")
Input here: 28
>>> print a
28
>>>

Haha...Subir you lied me, Python took the number 28 from the user, and displayed it as well.

Now, Did I really lie?? I think no, check this out:

From the above commands 28 is assigned to variable a

now lets assign a number to another variable, and try to add them out and check.

So lets do something like this:

>>> a = raw_input("Input here: ")
Input here: 28
>>> print "The Input from the user is: " + a
The Input from the user is: 28
>>> b = 20
>>> a + b

Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    a + b
TypeError: cannot concatenate 'str' and 'int' objects
>>>


See here, it gave us an error, because, when we use raw_input, Python thinks it as String type data, and not Integers.

and when we tried to add a and b, Python gave us a nice error, stating, dear user, i don't know how to add an Integer and a String.

So conclusion here is, whenever we want the user to input an integer type data, we use the input(), and when we want the user to input String type data we have to use raw_input()

Now lets try to build a small program with these knowledge:

Open IDLE>>File>>new(Ctrl+n)

=======================================================
#Program to check the usage of input() and raw_input()
name = raw_input("Enter your name here: ")
age = input("Enter your age here: ")
print name+", Your Age is "+`age`
=======================================================

Save it as name.py or anything you want, and run it

Display for me is:

>>>
Enter your name here: Subir
Enter your age here: 24
Subir, Your Age is 24
>>>

Well, so now i think its clear about the difference between input() and raw_input()

Here is a Task for you, "Write a Program that will ask the user his name, country, telephone number, and will display as given below: "

>>>
Enter your Name here: Subir
Enter Your Country Name Here: India
Enter Your Phone Number Here: 8004746836
Information successfully added to Database
Name Entered is: Subir
Country associated with Subir is India
Phone number entered by Subir is  8004746836
Press Enter to exit

Replace Subir With Your Name. If you can make this post it in the comment box below.

I hope you enjoyed this, practice them, and Don't Forget to check my next Post.

Thank You!

7 comments:

  1. name = raw_input("Enter your name here: ")
    country = raw_input("Enter your country name here: ")
    phonenumber = input("Enter your phone number here: ")
    print "Information succesfully added to database"
    print "Name entered is "+name
    print "Country associated with "+name+" is "+country
    print "Phone number entered by "+name+" is ",phonenumber
    raw_input("Press Enter to exit")

    ReplyDelete
  2. a=raw_input("Enter your name here: ")
    b=raw_input("Enter your Country here: ")
    c=input("Enter your Phone Number here: ")
    print "Information succefally added to database"
    print "Name Entered is: "+a
    print "Country associated with "+a+" is "+b
    print "Phone Number entered by "+a+" is "+str(c)
    raw_input("Press Enter to Exit")

    ReplyDelete
  3. hey i got L at the end of phone number ..why this happened???


    enter your namerakesh
    enter your countryindia
    enter your telephone number9998728515
    your details arerakesh
    india
    9998728515L

    ReplyDelete
    Replies
    1. input("Enter Number: ") << This takes int type data.

      So if u use input for phone number, while printing the output, u have to cast the datatype as string by str(object)

      Ex:

      ph1 = input("Enter Ph Num: ")
      print ph1
      #The above would show a last character as L means a Long Integer
      print str(ph1)
      #This would show the number without an L, because we have converted the data type to String.

      Delete