The input() function
We're now going to introduce you to a completely new function, which seems to be a mirro reflection of the good old print() function
Why? Well, print() sends data to the console.
The new function gets data from it.
print() has no usable result. The meaning of the new fucntion is to return a very usable result.
The function is named input() . The name of the function says everything.
The Input() function is able to read data entered by the user and to return the same data to the running program.
The program can manipulate the data, making the code truly interactive.
Virtually all programs read and process data. A program which doesn't get a user's input is a deaf program.
Take a look at our example:
print("Tell me anything...")
anything = input()
print("Hmm...", anything, "... Really?")
It shows a very simple case of using the input() function.
Note:
Try to run the code and let the function show you what it can do for you.
Try to run the code and let the function show you what it can do for you.
The input() function with an argument
The input() function can do something else: it can prompt the user without any help from print().
We've modified our example a bit, look at the code:
anything = input("Tell me anything...")
print("Hmm...", anything, "...Really?")
Note:
The variant of th einput() invocation simplifies the code and makes it clearer.
The result of the input() function
We've said it already, but it must be unambiguously stated once again: the the result of the input() function is a string.
A string containing all the characters the user enters from the keyboard. It is not an integer or a float.
This means that you mustn't use it as an argument of any arithmetic operation, e.g., you can't use this data to square it, divide it by anything, or divide anything by it.
anything = input("Enter a number: ")
something = anything ** 2.0
print(anything, "to the power of 2 is", something)