Ask me something! The Python input() function

When we create a Python program or script, we sometimes need additional information. This information might be details from the experiments (e.g., subject age, amplifier settings, etc), or it might be an answer to a specific question (e.g., “Accept trial? Y or N”). When you need to ask the user of your code a question, you need their input, you can use the Python input() function to do just that!

Asking the user to input an answer

Let’s create a simple script to calculate body-mass index (BMI). Because we are the only person using this script, we can hard-code the height and weight variables:

# Hard-coded variables
weight = 73    # in kg
height = 1.78  # in meters
bmi = weight/height**2
print(bmi)

Great! We now have a script that gives us the BMI value for people who weigh 73 kg and are 1.78 meters tall. But what if we wanted to calculate the BMI of any person who runs our script?

Python comes with an input() function that allows you to ask users to type an input at the command prompt. Let’s use this function to ask users for their weight and height:

# Now ask user for weight and height
weight = input('Weight in kg = ? ')
weight = float(weight)
height = input('Height in meters = ? ')
height = float(weight)
bmi = weight/height**2
print(bmi)

That was not too hard! Notice that we specified our question to the user with the text in parentheses (e.g., ‘Weight in kg = ? ‘). Also, we assigned the value of input() to a variable (e.g., weight).

Any value entered in response to input() is a string variable (i.e., text), even if a number is entered. For example, if we write x = 5 and run type(x), Python returns int. This is telling us that the variable x, which is equal to 5 in this case, in an integer number. However, if we write x = '5' and run type(x), Python returns str. This is telling us that y is a string variable. Because input() returns a string value, we have to convert the weight and height variables to the float type. Floating point numbers are those that have decimals. For example, 5.4 is a float; try typing type(5.4) in your Python terminal to convince yourself.

With our two inputs converted to numbers, we can compute the BMI of our user and then print this value to the command line.

input() vs. raw_input().
In Python 2.x, we would use raw_input() to get information from the user. In Python 3.x, the function was changed to input().

For those of us who like shorter code, we can re-write the above script as:

# A shorter version...
weight = float(input('Weight in kg = ? '))
height = float(input('Height in meters = ? '))
print(weight/height**2)

Make sure you take the time to understand what is going on here. The input() function is called within float(), which means that whatever the user writes will be converted to a floating point number. This value will then be assigned to the variable on the left of the = .

Summary

We learned how to get information from a user and use it to do some useful computations. This approach is very useful and I have used it in a variety of ways over the years. Another approach is to ask the user for information when they run the program. We will see this approach in the next tutorial.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s