User input
- Developers often have a need to interact with users, either to get data or to provide some sort of result.
- When you write a program sometime we need to give input from keyboard or any input device.
- Python provides us with two inbuilt functions to read the input from the keyboard.
a). raw_input(prompt)
b). input(prompt)
raw_input() function
- This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store.
- If you entered any number then these numbers also converted into string.
input() function
- This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether user entered a string or a number or list.
- If the input provided is not correct then either syntax error or exception is raised by python.
- Example : x=input("What is your name: ") Here, 'x' is a variable which will get the value, typed by user during the execution of program.
- command written in the double or single quotes inside the input function is printed as it is on the screen as shown below.Here you can see that program will print 'What is your name:' and waiting for the user to input the value.
- Program execuation will stop until user not given any input Here user enter the name. And this name value is stored in the variable 'x'.
- After giving the input hit enter button so that program continue their execution.
- Let's check whether x assigned the same value (user input) or not for this we use print function. Example : print(x) and we get the same output that the user input.
This is this simple way to take input from the user directly. you may take a list as an input. these all are discussed in upcoming posts.
If you have any problem regarding any topic till discussed in this series please let me known in the comment box.
Thank You.....
Top comments (0)