Transcript for:
User Interaction in Programming Basics

hello in this video you'll learn how to make programs that respond to you when you run them in this course we will refer to the person who runs your program as the user in the exercises you've been playing the part of both the programmer and the user you have been both the person writing the program and the person running it so far you've written programs that interact with the user in a one-way fashion your program does something it prints something end of story in this lesson you'll learn how to write programs that ask the user for information in order to do this in order to incorporate what the user types into our program we need a way to retrieve what they type and a way to store it we can use a variable to do the second thing but how do we do the first thing we can with the input function this is a function that prints a prompt and retrieves text from the user here's what it looks like alongside a variable and a print statement the function is called input and it requires you to include a string in parentheses this is the prompt that the user sees before they start typing what happens when you run this and we'll see this in a second is the interpreter actually pauses on this line and waits until the user typed something and presses the Enter key once the user is finished typing whatever they typed is stored in a variable called name then as we've done in other programs we can print out the variable name here's a slightly shortened version of the program we just saw and below it you can see roughly what happens when we run it notice that the type of the variable where the input is stored is ster or string this makes sense because the user entered a bunch of text but what if the user enters a number turns out the type of the variable is still ster this is a problem if you want to perform some mathematical operation on a number entered by the user the program at the right causes an error because the type of the variable number is ster and you cannot use the plus operator on astir and an INT fortunately python provides a handy function called int that converts a string into an integer as long as the original string really contains a number let's take a closer look at this line there's a lot going on here and it's easy to get overwhelmed by the parentheses the key is to evaluate from the inside out first the input function runs as it normally would Python prints of prompt and pauses while the user enters text then whatever the user types is given to the int function which converts it from a string into an integer finally that integer is stored in the variable called number let's step through what Python does assuming the user enters the number twenty two the input function prints the prompt and the user types two two and the Enter key Python then replaces the call to input with the string 22 the int function then takes the string 22 and turns it into the integer 22 this is what ultimately gets stored in number this process we just went through is what's known as function composition something you'll see more of as you continue programming when reading code that includes function composition just remember to evaluate from the inside out