Python User Input
Learn all about Python User Input in this comprehensive tutorial.
- •Python allows for user input.
- •In the example above, the user had to input their name on a new line.
- •You can add as many inputs as you want, Python will stop executing at each of them, waiting for user input:
- •The input from the user is treated as a string.
- •It is a good practice to validate any input from the user.
User Input
Python allows for user input.
That means we are able to ask the user for input.
The following example asks for your name, and when you enter a name, it gets printed on the screen:
Using prompt
In the example above, the user had to input their name on a new line. The Python input() function has a prompt parameter, which acts as a message you can put in front of the user input, on the same line:
Multiple Inputs
You can add as many inputs as you want, Python will stop executing at each of them, waiting for user input:
Input Number
The input from the user is treated as a string. Even if, in the example above, you can input a number, the Python interpreter will still treat it as a string.
You can convert the input into a number with the float() function:
Validate Input
It is a good practice to validate any input from the user. In the example above, an error will occur if the user inputs something other than a number.
To avoid getting an error, we can test the input, and if it is not a number, the user could get a message like "Wrong input, please try again", and allowed to make a new input:
Module quiz
2 questionsWhich of the following is true about Python User Input?
What is the most common pitfall when working with Python User Input?
Answer all questions to submit.