8.Getting user input
Python provides the input () method to retrieve user input. As the name implies, it reads all the characters you type in the environment until you hit the enter key. Consider the following application:
1 name = input("Enter your name: ")
2 age = input("Enter your age: ")
3 height = input("Enter your height: ")
4
5 #Print a blank line
6 print()
7
8 #Show the details you typed
9 print("Name is {0}.".format(name))
10 print("Age is {0}.".format(age))
11 print("Height is {0}.".format(height))
Result:
Enter your name: John Enter your age: 18 Enter your height: 160.5 Name is John. Age is 18. Height is 160.5.
We first define 3 variables to store data in the program (lines 1, 2 and 3). The application asks the user to enter his name (line 1). In line 2 you enter your name as a user.
Then the program asks us age (line 3). In line 6 we also create a dash by the print () method to create a gap between your inputs and outputs.
Now run the program and see the result by entering the desired values.