Introduction: Teach Me Python #4: Input Statements
So far in this tutorial series, we have created simple programs that do not require any user input. However today we will begin to learn about input statements which take a user input from what the person running the program types. As always please favourite, comment and subscribe
Step 1: The Code
After opening a blank python file type the following code:
userName = input('What is your name ')
How this works is that on the first line the computer is creating a variable called userName, what happens next is the variable userName gets filled with whatever is typed by the user at that point. The computer will wait until the enter key is pressed before continuing Which allows strings of multiple characters to be entered.
Whatever you type into an input statement it will always be an string. To create an integer you would type:
userAge = int(input('How old are you? '))
userAge = input('How old are you? ')
userAgeInt = int(userAge)
We will go over the use of int() later in the series
The syntax for an input statement is:
variableName = input('optional prompt')
You may have noticed in the first example how we put a few spaces at the end of the prompt, this is because otherwise it would force you to begin typing your input right up next to the last word of the prompt and as well how i say the prompt is optional because if you just type input() it will still request an input but it won't have the prompt there to tell you what to type.