Getting user inputs is an essential part of a program. In python we have several ways to get inputs like direct keyboard , arguments, environment variables etc. In this document I'll explain about keyboard inputs and arguments.
Input functionsFirst let's talk about input() functions. In python 2.x version there are two input functions called input() and raw_input(). But python 3.x only use input() function. There is no raw_input() function. Any way we want to learn about both of those two versions. Now I write a simple program to demonstrate input process.
First I used raw_input() function to get a word from user. We have used str() function hear. What it does? It convert what ever found as a argument to a string. Actually raw_input() function output a string . But it is good programming practice . So whenever you get a input convert it into the type you want. OK next we use input() function. This one will get input from user and try to interpret it as a valid python code .I use int() function to convert user-input to an integer. Finally we ran the program with python 2.x interpreter. You can see that works fine. Now we can't use raw_input() for python 3.x version. So we used input() function for both strings and integers. When we want to input an integer we want to use int() function. As well as if we expect a string from user we use str() function . Let's make a quick short summary of all of those. python 2.x uses input() and raw_input(). python 3.x uses only input() . Its good to use str() , int() and float() to convert input.
[email protected]:~/programming$ nano tmp1.py [email protected]:~/programming$ cat tmp1.py #!/usr/bin/env python x = str(raw_input('enter a string: ')) y = int(input('enter a value: ')) print ("you entered %s for x and %d as y "% (x , y)) [email protected]:~/programming$ python tmp1.py enter a string: Thilan enter a value: 21 you entered Thilan for x and 21 as y [email protected]:~/programming$ nano tmp1.py [email protected]:~/programming$ cat tmp1.py #!/usr/bin/env python x = str(input('enter a string: ')) y = int(input('enter a value: ')) print ("you entered %s for x and %d as y "% (x , y)) [email protected]:~/programming$ python3 tmp1.py enter a string: Dissanayaka enter a value: 1997 you entered Dissanayaka for x and 1997 as y
Using argumentsNow we are going to see how we can use arguments in our programs. Do you have used tools like SQLMap , nmap, dirb etc. Then you give arguments to those programs while run them. Like following. nmap -sv 192.168.56.101 Hear is our little script.
You can see , it's working g fine on both python versions. There are some points to note. First you can see we use a special command "import". Those are related with modules (or libraries) . from sys import argv We say import argv function from the module sys. We will talk more about this in a later tutorial. Yes when we calling the program we supplied some arguments. python tmp1.py Thilan Danushka Python think all things after the word 'python' are arguments so it thinks all these three strings ('tmp1.py' , 'Thilan' , 'Danushka' ) are arguments. :-). All of our argument string will save in a array called argv. So in our program we extract those from argv. Next there is a command. x,y,z = argv Hear we get arguments from argv and assign them to variables called x,y and z. So variable x will contain the string 'tmp1.py' , Also 'Thilan' is in y and variable z contains the string 'Danushka' . Next we can print them or do what ever we want with those arguments. You can't assume user always input what you expect. He/she may enter a string while you say to enter an integer. So if you try to convert a string to integer it will throw an error and program will give unexpected results. Hear something to clear up. I said if program try to convert a string to integer it'll lead to an error. Then what happen when we use int() function? think about following example. x = '2' y = 'dummy_string' Hear both of variable x and y are strings. But we can covert x into a integer using int(x). What happen if we try to convert y into a integer?
[email protected]:~/programming$ cat tmp1.py #!/usr/bin/env python from sys import argv x,y,z = argv print ('first argument is %s' %(x)) print ('second argument is %s' %(y)) print ('third argument is %s' %(z)) [email protected]:~/programming$ python tmp1.py Thilan Danushka first argument is tmp1.py second argument is Thilan third argument is Danushka [email protected]:~/programming$ python3 tmp1.py Thilan Danushka first argument is tmp1.py second argument is Thilan third argument is Danushka
To avoid this we want to use expectations and error handling. I'll explain this in another tutorial. Thanks for reading.
>>> x='2' >>> int(x) 2 >>> y='dummy_text' >>> int(y) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'dummy_text'