This article discusses handling user input in Python. For any program that interacts with someone, the user interface (UI) is important.
User Friendly Interface
The key to a good UI is that it guides the user for any required entry and clearly explains any results.
First, it is always helpful if a program explains what it is for. For basic programs in Python, this can be accomplished by simply displaying a "banner", such as the following:
For SDEV140, the banner should include the programmer name or initials:
A "prompt" asks the user to enter information. A good prompt clearly explains what is needed, and guides the user in what information to enter. Here is a not-so-great prompt:
Amount of what? Should it be a whole number, with decimals, or something else?
Here is a much better prompt:
This is even better:
Rules for a good UI
- Explain clearly what is needed
- Be polite, if appropriate
- NEVER, NEVER assume the user knows what to do
- Guide the user in how to use the program and enter information
Never assume the user knows what to enter or how. Always explain and guide the user is any way possible. Unless the assignment requirements say otherwise, use multiple lines, if necessary, to assist the user.
Clearly informing the user of the program results is also important. This result would not be very helpful:
It is not obvious what the results represent. Is it an amount? OR a percentage? Based on what? YES, the results should be explained so that they are OBVIOUS!. Do not assume the user (even the instructor) knows what your results mean.
Here is a better output:
This result at least explains that the output is the sales tax, and that it is a dollar amount.
Here is a much better and clearer output:
Calculated Sales Tax (5%): $23.47
Total Amount Due: $483.87
Thank you for using the program.
Notice that there are multiple benefits of this last output:
- The user can confirm that their entry was correct
- The end result (the sales tax) is clearly stated
- The user can confirm the program worked correctly (because the percentage is also displayed)
- Although it may not have been a requirement, the total due provides the user with extra, valuable information
Handling User Input in the Code (Advanced)
Many sample Python programs will show a sample input statement like this:
While this is acceptable for a first assignment from a beginning programmer, it is poor programming for one main reason. What if the user enters "ABC"? The code fails. User entry is ALWAYS a string. If the program needs a number, the string entry must be converted. In beginning programs, it is usually assumed the user knows what to do, and will enter numbers when needed. But, it is a bad practice to get into the habit of automatically converting user input to numbers.
Verifying User Input
Introductory modules and beginning exercises do not require checking user input. The focus of those lessons are on learning other parts of Python. However, depending on the type of data needed, simple validation of input is not difficult. It takes several lines of code to properly complete the validation, but the logic is simple (the same block of code can be used in different ways in many different Python programs). Examine the following code:
This sample uses two techniques to validate the user input. First, it uses a while loop to continue asking the user for input until it is correct. Since the user may not be able to enter a valid value (or does not want to), the program needs to give the user a way to abort the program. This is indicated in the prompt, and by checking for the abort value.
Second, the program uses a series of "if" statements to check for a blank entry and for a valid number. Python has a set of functions such as isdigit(), isnumeric() and isdecimal() that can be used to check for whether a string is a number. See this article for more explanation.
In this case, once the program leaves the loop, the code checks to make sure the user did not want to exit, and then converts the entry to a number to proceed.
Additional Validation Checks
In some cases, it makes sense to perform additional validation checks on user input. Though the user may enter a number, for instance, is it a valid number for the purposes of the program? For example, entering a negative number would not be valid for an age, a mortgage or a purchase amount.
The code below adds two more "if" statements to check that the user entry is a valid age. In this case, once the user entry is determined to be a number (and not the abort code), the entry is converted to an number inside the while loop, so that the value can be validated within a certain range. Like the other statements, if the entry is not valid for these extra checks, the entry is cleared and the user prompted again for a valid entry.