Converting Keyboard Input

00:00 In this lesson, you will learn how to convert the keyboard input that you get using the input() function into an integer so that you can make some numerical calculations with it.

00:12 When I head back over to this small script from before where you were collecting the name of a user, now instead of connecting the name, let’s collect the age and then do a small calculation with it.

00:25 And, actually, let’s take a look first in the Python interpreter what you might be expecting to happen. You want something like this, that you ask the user to input their age—let’s say they’re thirty-five years old—and you collect this input and then you want to make some calculation. For example, you want to tell them how old they are going to be in fifty years, and then that would be 85, right?

00:48 So, this is kind of what you want to mimic

00:53 in your script.

00:55 You’re collecting the age from the user. Let’s adapt the prompt to reflect that. I’m going to say "What's your age: ", and then afterwards you want to say age + 50.

01:08 You see there’s some squiggly red line here. This is because the Visual Studio Code editor already tells you that there’s going to be a problem happening with that. You can inspect that as well, but let’s just run it anyways, just so that you see what error comes up.

01:22 So if I say python, press Enter, Python again opens up the space for me to give some user input here. I’m going to say 35, press Enter, and then you get this TypeError where Python tells you it can’t concatenate a string and an integer object together. So, you know that this is an integer object, and this error already gives you an indication of what’s happening with the input() function, which is that it always returns a string.

01:51 You can see it also here in the type annotations. If you use the input() function, it will always return a string, so if you want to do some sort of calculation with it afterwards, you need to first convert it to the type that you need.

02:04 You can do that by wrapping it inside of the integer conversion function here. I can say, “Collect that input,” it’s going to be a string "35", and then the int() function that you’re wrapping around this input() is going to convert it to an integer and then assign that integer to the age variable.

02:23 Then, afterwards, this calculation is going to work fine. So now, if I run the same script again, give the same input, then you see there is no error and the script finishes just like it should.

02:38 So, to remember here is that if you collect input using the input() function, then it’s always going to be a string. And if you want to do some numerical calculations, for example, you first need to convert that input to whatever type you need.

02:52 In the next lesson, you’re going to make this script a bit more useful because currently you’re only doing the calculation, but you’re not actually displaying anything back to the user, and so you will learn how to use the print() function to display some output back here to the console.

Become a Member to join the conversation.