Get user input
Most of the applications you write will need some way to interact with a user. The simplest way to obtain user input is by using the input() function. This function prompts the user for an input from the keyboard. Once the user has give the input and pressed Enter, the program flow continues.
Here is an example:
myName = input('What is your name? ') print('Hello', myName)
The code above will produce the following output:
>>> What is your name? Mark Hello Mark >>>
The input() function will always output a string. If you want a number, you will need to convert the user input to a number using functions such as int() or float().
Another example of the input() function:
x = 5 y = int(input('Type a number: ')) z = x + y print(y, 'plus 5 equals', z)
The output of the code above:
>>> Type a number: 3 3 plus 5 equals 8 >>>
One more example:
name = input('What is your name? ') x = int(input('How old are you? ')) if x >= 21: print(name,'you are old enough.')
The output, if we enter the name Mark and the number 22:
What is your name? Mark How old are you? 22 Mark you are old enough.