#3 Input Function

Previous Post: https://codinghelp.site/knowledgebase/python/print-function/

Next post: https://codinghelp.site/knowledgebase/python/4-variables/

The “Input()” Function

Input is a built-in python function. It is used to get users input in the terminal/console.

The “input()” Function Syntax

This image has an empty alt attribute; its file name is Zbd1OLGymNcePFtdmi_01AD_USmvtYco4Ww-Uf3-80Aj0oORAITk2VtAouRl4KJqh_No6Ym9bYGkb1ibEwKxZRluw8LJSTN9fn2Z0QWhYYOD3P4cbNYw02XnmYdfPAMb9tXQFou9

Here, “__prompt” is representing a string that we have to pass in the parentheses of the INPUT() function. So for an example:

This image has an empty alt attribute; its file name is QZ-60x9eiCocsqVl9GYKNtCn7PaCcnfCXvTSCaki9KTek99a222vQh4lRFkDzcYAE9b3vpIeRUl3qMlN69z6FuEK1ZocFGcxwH8QGFE1hEekMTyAfHqqFDPf5toaYsocgg6kzJQc

In the console it would look something alike…

This image has an empty alt attribute; its file name is e4KYOW-3Y9clMXKdRiuNTMeVMTfrA_ZErVozKVYlDXALn-Z_okP0fa9pkAicdrX9FWQIWFuxxx9mE7MKGjxo_ejEg9E0bT7OSWKPdxlPWlfC62YDLclBom4LCEzZComxpCsYKxk6

Light green text is representing the user’s choice of input in the console. The function is usually put inside a variable, so we can retrieve the output.

Example

This image has an empty alt attribute; its file name is QyVs77pOatVx2HJ1tnqhMu1canzv1KkmRiCURR98yj0a5ZvYEIBrwwTJh3FMUqKb4m9uUJqT6zP7hGKMdCJdOb-mwa9WGAKjEgOb9bXT7287Tcjf7qxBHJvumQwG-ssdvLDeo5yc

Here, we are assigning the value of whatever the user inputs to a variable named “name”. After the user inputs something, we are printing “Hello __entered name__, nice to meet you!”

__entered name__ is representing the users input in the console at the input function.

Exercise

Make a script that asks the user for their age then tell them how old they will be in 100 years. Make sure to make the variable an integer!

Answer key

image

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.