Python - User Input

04.21.2020

In this tutorial, we will take a quick look at taking input from the user. Typically, you will only use this when writing command line apps, as when writing something like a web app the user input will come from elsewhere.

Alright, let's start with an example.

userName = input("What is your name? ")
print(userName)

That's it. By using the input function, we get anything the user types.

Let's take a look at something specific to Python. Basically, if we are running a python file, we want to check of that is the correct intention of the user. They could also be loading the file to use as a library (we will learn more about these later).

Here is an example:

if __name__ == "__main__":
    userName = input("What is your name? ")
    print(userName)

Here we are checking if the magic value **name**is equal to "main" which is the python way of telling use the user is trying to run this from the command line.

References