Skip to content

User Input

User Input

To begin this chapter, Python's input() function is discussed. Python can be used to ask users for input. The input entered by a user can be saved to a variable and used in subsequent parts of the program. The syntax of Python's input() function is below:

var = input('message')

Where var is the variable that stores the user's input and 'message' is the message the user sees at the prompt. A string enclosed in quotes, like 'message', needs to be passed as an input argument to the input() function. Let's ask a user for their age:

In [1]:
age = input('how old are you? ')

how old are you? 9

Since the user's input is assigned to a variable, further operations can be run on it. Now, let's print the user's age back to them. This can be accomplished with an f-string. Note the f' ' inserted before the string. A set of curly braces { } surrounds the variable's name and the variable's value is printed back to the user.
In [2]:
age = input('how old are you? ')
print(f'you are {age} years old')

how old are you? 9
you are 9 years old

Let's try another example. We will we ask the user for the base and height of a triangle and print out the area of the triangle.

But, there is a problem with the approach below. The code block does not run because a common error is present.

In [3]:
b = input('base of triangle: ')
h = input('height of triangle: ')
A = (1/2)bh
print(f'The area of the triangle is: {A}')

base of triangle: 5
height of triangle: 2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-c9cb8f02e604> in <module>
      1 b = input('base of triangle: ')
      2 h = input('height of triangle: ')
----> 3 A = (1/2)*b*h
      4 print(f'The area of the triangle is: {A}')

TypeError: can't multiply sequence by non-int of type 'float'

The previous section of code returns an error because of the data type of the variables b and h. We can investigate b and h's data type with Python's type() function.
In [4]:
b = input('base of triangle: ')
h = input('height of triangle: ')
print(f'b and h are of type: {type(b)}, {type(h)}')

base of triangle: 5
height of triangle: 2
b and h are of type: <class 'str'>, <class 'str'>

Notice both b and h are strings, even though the numbers 5 and 2 were entered as input. The output of the input() function is always a string, even if the user enters a number.

To complete the area calculation, b and h first need to be converted to floats using Python's float() function, then the mathematical operation will run without error:

In [5]:
b = input('base of triangle: ')
h = input('height of triangle: ')
A = (1/2)float(b)float(h)
print(f'The area of the triangle is: {A}')

base of triangle: 5
height of triangle: 2
The area of the triangle is: 5.0

Now that you are familiar with Python's input() function, let's utilize a user's input to decide which lines of code will run. The concept of an selection statement is introduced the next section.