The Input Command

The input command is necessary when we want to obtain input from a keyboard. So far, variables have been initialized or assigned values within a given Python script, with commands such as:

a=3
b=27
c=a+b

It is often the case that a program requires keyboard input from the user to perform its function. This is exactly what the 'input' command is for. This instruction can output a message, and the program will wait for the user to input a value. Try running this command in the Repl.it run window:

city=input('Enter the name of the city where you are located: ')
print(city)

You should be aware that the input function returns data of type str (that is, string data). If numerical data is required, an extra step must be taken to perform a type conversion using either the int or float commands. Copy and paste this set of commands into the run window and then run code using an input value of 34 when prompted for user input:

temperature_str=input('Enter the temperature: ')
print(temperature_str)
print(type(temperature_str))
print()
temperature_int=int(input('Enter the temperature: '))
print(temperature_int)
print(type(temperature_int))
print()
temperature_float=float(input('Enter the temperature: '))
print(temperature_float)
print(type(temperature_float))
print()

The variable names have been chosen to emphasize and distinguish between their data types. The input command offers the convenience of creating user-defined values within a program. At the same time, it is important to make sure the input value's data type matches the intended use of the variable.

The programs we have written so far accept no input from the user. They just do the same thing every time. 

Python provides a built-in function called input that stops the program and waits for the user to type something. When the user presses Return or Enter, the program resumes and input returns what the user typed as a string. In Python 2, the same function is called raw_input.

>>> text = input()
What are you waiting for?
>>> text
'What are you waiting for?'
Before getting input from the user, it is a good idea to print a prompt telling the user what to type. input can take a prompt as an argument:
>>> name = input('What...is your name?\n')
What...is your name?
Arthur, King of the Britons!
>>> name
'Arthur, King of the Britons!'
The sequence \n at the end of the prompt represents a newline, which is a special character that causes a line break. That’s why the user’s input appears below the prompt.

If you expect the user to type an integer, you can try to convert the return value to int:
>>> prompt = 'What...is the airspeed velocity of an unladen swallow?\n'
>>> speed = input(prompt)
What...is the airspeed velocity of an unladen swallow?
42
>>> int(speed)
42
But if the user types something other than a string of digits, you get an error:
>>> speed = input(prompt)
What...is the airspeed velocity of an unladen swallow?
What do you mean, an African or a European swallow?
>>> int(speed)
ValueError: invalid literal for int() with base 10
We will see how to handle this kind of error later.


Source: Allen B. Downey, http://greenteapress.com/thinkpython2/html/thinkpython2006.html#sec65
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial 3.0 License.

Last modified: Tuesday, November 17, 2020, 3:15 PM