Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

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: 

  • The program prompts the user to input some data from the console (most likely using a keyboard, although it is also possible to input data using voice or image);
  • the input() function is invoked without arguments (this is the simplest way of using the function); the function will switch the console to input mode; you'll see a blinking cursor, and you'll be able to input some keystrokes, finishing off by hitting the Enter key; all the inputted data will be sent to your program through the function's result;
  • note: you need to assign the result to a variable; this is crucial - missing out this step will cause the entered data to be lost;
  • then we use the print() function to output the data we get, with some additional remarks.

Try to run the code and let the function show you what it can do for you.

  • The program prompts the user to input some data from the console (most likely using a keyboard, although it is also possible to input data using voice or image);
  • the input() function is invoked without arguments (this is the simplest way of using the function); the function will switch the console to input mode; you'll see a blinking cursor, and you'll be able to input some keystrokes, finishing off by hitting the Enter key; all the inputted data will be sent to your program through the function's result;
  • note: you need to assign the result to a variable; this is crucial - missing out this step will cause the entered data to be lost;
  • then we use the print() function to output the data we get, with some additional remarks.

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 input()  function is invoked with one argument - it's a string containing a message
  • the message will be displayed on the console before the user is given an opportunity to enter anything
  • input() will do its job

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)

The input() function

David Khieu
Module by David Khieu, updated more than 1 year ago

Description

The input() function
No tags specified