In this video, learn how to use Python's built-in input and print functions to get input from the user while the program is running, and give output back based on given conditions.
- Throughout the course, we've been outputting information to the user, even if we didn't know it. We've been using the print statement, and so we just go print, and then whatever we want to print or output to the user, and so we can say hello to the user. In a previous video, we got initial input from the system module and the command line arguments. But what if we need input from the user throughout the program's execution? That's where the built-in input function comes in. To use it, we'll just go input, and then whatever we want to prompt the user to input.
So say we want the users favorite color. Well, we would write, What is your favorite color? And then the user would input their favorite color. Now this is great to get that color, but we're not saving it anywhere. If we want to use the users favorite color somewhere else in the program, we need to save it in a variable. So here, we'll say color and that will save whatever the user inputs as their favorite color. So if we run this, we see hello as our output, and then it says, What's your favorite color? I'm just going to put in red, and here it's important to remember that the top part here is our code.
And that code is what determines what will go into the output of our program. The output is what the user is going to see. So all of these print statements, what they actually print, that's what the user sees. And so, when I wrote in red here, I was acting like the user and that the user would first see What is your favorite color, and then I, as the user wrote red because I was responding to the question, What is your favorite color? And now that's saved in the program but what we don't do anything else with it. Well, we can go back and use our print statement from before and just print out what the user gave to us.
And so we could go print, color. And this will print out whatever the user responds to us. And so, running this, favorite color, we'll say pink here, and it's going to return pink because that's what the user put in. Whatever the user puts in determines what is outputted by the program, according to what we have here. This is great for when you need information from the user or you need to give the user feedback throughout the programs execution. However, unless saved elsewhere, the data generated from this program will disappear after the program's completion.
So here, if we run the program again we're not going to have access to pink anymore, or whatever color that the user responds with. We need to save this color elsewhere in a file or somewhere else so that way we can access it in different executions of this file.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content