 In this video, we're going to talk about the input function. Let's look at this program that we wrote in the print video. One thing I'll note is that I modified the program, and I forgot to modify the description. Not only is it given an age in years, it is given a person's name and their age in years. It calculates the approximate age in days and gives personalized output. This more accurately reflects what the program does. And this program works. There's absolutely nothing wrong with it. If I run the program, it gives me the correct output. However, it's inflexible. Not everybody's named David, and not everybody is 64 years old. And in fact, next year, I won't be either. What we'd like is a way for the user to be able to take the program and run it and give us the information rather than having to modify the program every time they want to see new results. The way you do that is with the input function. I'm going to take this line and move it up here. Because when I run the program, I want to ask the person for their name first, and then ask them for how old they are, and then do the calculation. So instead of saying your name equals David, I'm going to use the input function. Inside the input function, I'm going to put something called a prompt. It's the question that I'm going to ask the user. What is your name? This is always going to be a string. Let's run this and see what happens. Remember what happens on line 6. Whenever I see this symbol, I have to work out the right-hand side first. So the input function is called do its thing. The input function puts the prompt up on the screen, and then waits for you to type something. I'll type David, because that is my name. I'll say David, your age and days is approximately 23, 360. Let's run that program again. This time I'm going to say my name is Joe. I'll say Joe, your age and days is approximately 23, 360. Now I've personalized the name by asking the user for input. I want to run the program one more time and point something out. Notice that the cursor for input is right up next to that question mark, and that looks a little bit ugly to me. Let me just put in somebody's name, Nancy. Normally I like to put a blank before the closing quote mark but the blank will show up on the screen. When the blank shows up on the screen, it gives the cursor some breathing room between the prompt and where the user will type their input. Running it again, what's your name? Federica. So far so good. Let's do the same thing for years. Let's make that an input, and we'll ask how old are you? Again, I'll put the blank at the end of the prompt just to give my cursor some breathing room. Save this and run it again, and it's going to ask what is your name, and this time I'll put in the name Martin. How old are you? And I'll say 20. Martin, your age and days, whoa, what the heck happened there? What happened there is strings. Whenever you do input, the result of the input function is always, always, always a string. When we asked what is your name, that was a string and no problem. When we asked how old are you, instead of getting the number 20, we got the string 20, and that's what years referred to. On this line, when we evaluate the right-hand side, we're multiplying a string by a number, and as you may recall from a few videos back, when you multiply a string by a number, it repeats it that many times. So we got 365 replications of the string 20. That's why we have the age and days as approximately 20, 20, 20, 20, 20, and no, I'm not going to read them all. How do we get around that problem? The answer is conversion to integer. The years is an integer, so what we will say is int of input of how old are you. What we have is a function calling a function, and let's go through this in detail to see what this computer is doing. When it sees line seven, it sees the symbol and says, oh, I have to figure out what the right-hand side works out to. The right-hand side parentheses come first in function calls, so at first does the input call. The input call puts up the words, how old are you on the screen, and I typed in 20. That brings back the string 20. That part is evaluated, and now that string 20 is passed on to the int function, and I get a number 20 as the final result of the right-hand side, and years will refer to the number 20. Let's clear the shell and run this again, and let's have Martin be 20, and now we get a rational result because we've converted the string from input into a number, which is what we really want to use for multiplication. This is something that's very easy to forget, and if it happens to you, you will not be the first person who has ever done it, and you will not be the last person who will ever do it. Don't freak out about it. I've known some beginning programmers who look at this and say, whoa, functions inside of functions, that's a little bit weird. I'd like to do it in two steps. That's perfectly fine with me. I could say years as string, oh, I'm sorry, I'm using the convention from a previous programming language, years as string equals input of how old are you, and that will give me a string, and then I can say the years is the integer form of years as string. It's one extra step, but if it helps you to understand better what your program is doing, then by all means, feel free to do it that way. You'll see that it has the same results. If I have someone named Henrietta, who's 45 years old, it'll still work great. Let's give the discount program the same treatment where we're going to ask for input so that it's not as inflexible. The description is going to be exactly the same, but in this case, instead of saying the amount is 1995, we're going to ask for input. Let's do it in two steps for this first one. Instead of 1995, we're going to input what is the price. In this case, I won't put the extra blank because having the cursor right up next to the dollar sign looks really good, and then the amount will be float of amount string. Remember, I need to have a floating point value with decimal points to represent money. I'll do the percentage as one step. It's the float of whatever input what is the discount percentage. And here, again, I have a nested function call. Let's run the program. When I test it, I like to use numbers where I know exactly what the answer is going to be in advance to make sure everything is working properly. If something costs $50 and a 10% discount, I should get $5. This gives me confidence that my program is working correctly. I can run it again, and now I can use any price I want with any discount I want. If something costs $45.99, and I have a 3.25% discount, I'm saving $1.49. Moral of the story, the input function puts up a prompt on the screen, waits for the user to type their answer, and press Enter, and whatever string they entered is returned to you. If you want a string, leave it as a string. If you need an integer or a float, remember to convert it. Otherwise, your arithmetic is going to not work the way you thought it would.