User Input (text based) in Python (basic). Mastery 28.

Programs in Python are able to receive input coming from the user. The function able to do this is the input function. When this function is called, the program stops and waits for the user to type something. The program continues when the user press return or enter.

Example:

After input(), the Python shell received the text that I wrote and that piece of text was assigned to the variable text, so when I print the text variable I am able to read the same text that I typed.

Also, you can prompt to the user what to input, per example:

If you want to see the input in a new line, use the sequence n at the end of the prompt. Example:

Can you see the difference? In my opinion it looks nicer.

If you expect the user to type a certain type, you can indicate that in your code. Example:

But if I type something different than an integer I get an error message:

So, this is basically how you handle basic input from the user.

28

 

 

 

 

 

CC BY 4.0 User Input (text based) in Python (basic). Mastery 28. by Ismael Lizárraga is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.