☰
Python Across Disciplines
with Python + AI Tool   
×
Table of Contents

1.1.   Introduction 1.2.   About the Author & Contact Info 1.3.   Book Conventions 1.4.   What (Who) is a Programmer? 1.5.   Programming Across Disciplines 1.6.   Foundational Computing Concepts 1.7.   About Python 1.8.   First Steps 1.8.1 Computer Setup 1.8.2 Python print() Function 1.8.3 Comments
2.1. About Data 2.2. Data Types 2.3. Variables 2.4. User Input 2.5. Data Structures (DS)         2.5.1. DS Concepts         2.5.2. Lists         2.5.3. Dictionaries         2.5.4. Others 2.6. Files         2.6.1. Files & File Systems         2.6.2. Python File Object         2.6.3. Data Files 2.7. Databases
3.1. About Processing 3.2. Decisions         3.2.1 Decision Concepts         3.2.2 Conditions & Booleans         3.2.3 if Statements         3.2.4 if-else Statements         3.2.5 if-elif-else Statements         3.2.6 In-Line if Statements 3.3. Repetition (a.k.a. Loops)         3.3.1  Repetition Concepts         3.3.2  while Loops         3.3.3  for Loops         3.3.4  Nested Loops         3.3.5  Validating User Input 3.4. Functions         3.4.1  Function Concepts         3.4.2  Built-In Functions         3.4.3  Programmer Defined Functions 3.5. Libraries         3.5.1  Library Concepts         3.5.2  Standard Library         3.5.3  External Libraries 3.6. Processing Case Studies         3.6.1  Case Studies         3.6.2  Parsing Data
4.1. About Output 4.2. Advanced Printing 4.3. Data Visualization   4.4  Sound
  4.5  Graphics
  4.6  Video
  4.7  Web Output
  4.8  PDFs & Documents
  4.9  Dashboards
  4.10  Animation & Games
  4.11  Text to Speech

5.1 About Disciplines 5.2 Accounting 5.3 Architecture 5.4 Art 5.5 Artificial Intelligence (AI) 5.6 Autonomous Vehicles 5.7 Bioinformatics 5.8 Biology 5.9 Bitcoin 5.10 Blockchain 5.11 Business 5.12 Business Analytics 5.13 Chemistry 5.14 Communication 5.15 Computational Photography 5.16 Computer Science 5.17 Creative Writing 5.18 Cryptocurrency 5.19 Cultural Studies 5.20 Data Analytics 5.21 Data Engineering 5.22 Data Science 5.23 Data Visualization 5.24 Drone Piloting 5.25 Economics 5.26 Education 5.27 Engineering 5.28 English 5.29 Entrepreneurship 5.30 Environmental Studies 5.31 Exercise Science 5.32 Film 5.33 Finance 5.34 Gaming 5.35 Gender Studies 5.36 Genetics 5.37 Geography 5.38 Geology 5.39 Geospatial Analysis ☯ 5.40 History 5.41 Humanities 5.42 Information Systems 5.43 Languages 5.44 Law 5.45 Linguistics 5.46 Literature 5.47 Machine Learning 5.48 Management 5.49 Marketing 5.50 Mathematics 5.51 Medicine 5.52 Military 5.53 Model Railroading 5.54 Music 5.55 Natural Language Processing (NLP) 5.56 Network Analysis 5.57 Neural Networks 5.58 Neurology 5.59 Nursing 5.60 Pharmacology 5.61 Philosophy 5.62 Physiology 5.63 Politics 5.64 Psychiatry 5.65 Psychology 5.66 Real Estate 5.67 Recreation 5.68 Remote Control (RC) Vehicles 5.69 Rhetoric 5.70 Science 5.71 Sociology 5.72 Sports 5.73 Stock Trading 5.74 Text Mining 5.75 Weather 5.76 Writing
6.1. Databases         6.1.1 Overview of Databases         6.1.2 SQLite Databases         6.1.3 Querying a SQLite Database         6.1.4 CRUD Operations with SQLite         6.1.5 Connecting to Other Databases
Built-In Functions Conceptss Data Types Date & Time Format Codes Dictionary Methods Escape Sequences File Access Modes File Object Methods Python Keywords List Methods Operators Set Methods String Methods Tuple Methods Glossary Index Appendices   Software Install & Setup
  Coding Tools:
  A.  Python    B.  Google CoLaboratory    C.  Visual Studio Code    D.  PyCharm IDE    E.  Git    F.  GitHub 
  Database Tools:
  G.  SQLite Database    H.  MySQL Database 


Python Across Disciplines
by John Gordon © 2023

Table of Contents

Table of Contents  »  Chapter 3 : Processing : Repetition : Validating User Input

Validating User Input

Subscribe Contact


Contents

Overview

We have been using the Python input() function for a while now and you may have noticed or wondered, what if the user enters a value that is not what we need? Say we prompt them for an integer, but they type in a string value and press Enter. What happens? Well, the answer to that is, it depends.

Let's take a look at an example:

age = input("Please enter your age: ")

By now you know that this line of code prompts the user for a value, in this example, we're asking the user for their age. We also know that anything they type at the prompt will be read by Python as a string, and what they type will be stored in the variable age. So far so good.

There are some potential problems with this, however. What if the user types ABKDJFHGIWYSDFGkk and presses Enter? Or, what if the user types 465464 and presses Enter? Both of these entries are invalid. The first entry is invalid because age is an integer value, and the entry is not a number at all. The second entry is invalid because 465464 is not within a valid range for a human age. So what do we do about these problems?

Validating Data

Concept: Data Validation
Full Concepts List: Alphabetical  or By Chapter 

Data validation in programming is the process of ensuring that the input data a program receives meets specific criteria and is within acceptable parameters before it is processed or used in computations. This process is crucial for maintaining the integrity and reliability of a program. Validation can involve checking for data type correctness (ensuring an input is an integer when an integer is expected), range constraints (such as verifying dates fall within a certain period), format correctness (like validating email addresses or phone numbers against a specific pattern), and presence of necessary data (ensuring required fields are not empty). Data validation helps prevent errors, inconsistencies, and security vulnerabilities by catching incorrect or malicious data before it can cause problems in the system. It is an essential aspect of robust software development, particularly in applications involving user input, file handling, or network communications. By incorporating thorough data validation routines, programmers can ensure their applications behave predictably and handle invalid or unexpected inputs gracefully.

Data validation is a large in-depth topic, so for now we'll keep it simple. As a programmer, it is your responsibility to validate all data that your program receives from any source as best you can. There's an old phrase that applies here: garbage in garbage out. In our context, this means that if we allow invalid data into our programs, files, databases, etc., when we need to use that data for important processes such as transaction processing, customer records, financial calculations, etc. our results will be poor.

Let's take a look at the two problems identified above and discuss possible approaches to validate the data after the user enters it into our programs. Invalid Data Types: The first problem we identified above about a user's entry for our age prompt is related to invalid data types. Even though the input() function accepts any data type entered as a string, ultimately the age must be a numeric and even more specifically an integer. Python provides some string methods to help us determine the contents of a string. You can find a full list of string methods on the String Methods Reference page. On that reference page, you'll notice a group of methods that start with "is", like isalpha(), isnumeric(), etc. These are often referred to as the is methods. This group of methods evaluates a string and returns True or False if the string contains the type of data indicated by the method name. So, for example, if our string is s = "Bob" then isalpha() will return True because all characters in the string are alphabetic. If s was "Bob5", or "Bob.Smith", or "Bob ", etc. then isalpha() would return False.

Returning to our age example, one aspect of a valid age is that it is an integer with no decimal portion or other characters. If we look at the list of string methods, we see isdigit() which returns True if all characters in a string are digits (no decimal points or other characters). We can confirm this will work for our purposes with a simple prompt and decision statement:

Code Example:

age = input("Please enter your age: ")
if age.isdigit():
    print("The string contains all digits")
else:
    print("The string does not contain all digits")

Code Details

Output (after program runs 3 times):

Please enter your age: 25
The string contains all digits

Please enter your age: 25.5
The string does not contain all digits

Please enter your age: adsfasdf
The string does not contain all digits

Output Details

(Almost) Full Data Validation Solution Using isdigit()

Now, let's use isdigit() to add data validation to our age example. By adding this functionality, the user will be prompted for the age, and then we will use a loop to test their entry. If they entered a valid integer we will continue, otherwise, we will repeatedly prompt the user until they provide a valid integer.

Code Example:

age = input("Please enter your age: ")
while not age.isdigit():
    print("Error: Invalid age, please try again.")
    age = input("Please enter your age: ")
print("Your age is", age, sep=" ")

Code Details

  • On Line 1 we prompt the user for the age.
  • Line 2 initiates a while loop with the not age.isdigit() signature. At first glance, this might not seem intuitive, but if we convert this to an English sentence it could be read "while the age variable is not all digits repeat the following...".
  • If execution enters the loop, it means the user's entry contains characters in the string that are not integer digits, so we display an error message and repeat the age input() function prompt.
  • Execution will stay inside the loop until the user enters a valid integer value.
  • Once the user enters a valid integer value, execution exits the loop and prints the user age result.

Diagram Details

  • The diagram depicts the flow of execution through the above code.
  • Notice the manual input symbology for the age variable, as well as for the printing of the error and age messages.

Why can't we just use the int() function?:

One question you may have at this point is why can't we just use the int() function to convert the user's input into the integer value? Like this:

age = int(input("Please enter your age: "))

Without data validation, that will only work if the user enters a valid integer value. If the user types in any data type other than an integer (like string, float, etc.) the user will have a run-time error which is something we as programmers must try to avoid. We would want to convert the user-entered age (string) value to an integer at some point, however, we would do this after it has been validated.

Now ... A More Complete Data Validation Solution

Even after everything covered above, we still have not addressed the other problem related to the age variable. What happens if we implement the data validation above but the user enters a value like 56465 as the age? It is an integer, but it is still invalid because human age does not reach that high of a number. So, in addition to the data validation code, we used earlier, now we need to add range boundary validation. This type of data validation is commonly referred to as boundary testing.

Using the age example, the boundaries of age for humans is, commonly, greater than zero and less than 125. The oldest known person in history was 122 years old, so an upper boundary of 125 is reasonable, should cover all cases, and avoid outrageously large age values. So, let's add a boundary test to our solution. This time I will introduce a couple of new concepts and while loop options that help us with this type of validation.

Concept: Exceptions

When a program encounters an unexpected condition the result is called an exception. If the programmer has not accounted for exceptions then the program could crash, display confusing errors to the user, produce invalid results, etc. There are a large number of causes of exceptions, such as invalid data, missing variables, improper syntax, missing files, out-of-memory, and many others. The process of writing code to capture, prevent and/or resolve exceptions is called exception handling.

Try-Except

Concept: Try-Except

Many programming languages include a try-except structure, which allows programmers to capture errors and exceptions, and respond to them more elegantly than just allowing their program to crash. In Python, the actual syntax of this exception handling concept is called try-except.

The general form of the Try-Except exception handling structure in Python is as follows:

try:
    Statement(s)
except:
    Statement(s)
else:
    Statement(s)
finally:
    Statement(s)

Code Details:

Example: Division by Zero

If you recall we learned previously that division by zero in computers is undefined. A common use of try-except is to handle the possibility of division by zero. In this example, I'll demonstrate this issue by prompting the user for two values and then dividing one from the other. Since I cannot control what the user enters, I'll use a try-except to catch the division by zero exception.

The following code is my first attempt at a solution.

x = int(input("Please enter first integer: "))
y = int(input("Please enter second integer: "))
try:
    z = x / y
except ZeroDivisionError:
    print("Error: You cannot divide by zero,\nso, your second integer cannot be zero.")
else:
    print("The result of dividing your first integer by the second is", x / y, sep=" ")
finally:
    print("Done.")

Code Details:

And now ... Let's Finish the Age Data Validation Solution Using try-except
Also, we'll introduce the break and continue statements as well...

Now that I have introduced you to the try-except construct, we can add it to our evolving solution to the age variable problem from above. As a reminder, we identified two problems with prompting the user for their age: the data type and the range of human age. We solved the data type issue with the isdigit() function. And we began discussing the issue of accepting only a range of valid values for human age (0 thru 125). Now let's incorporate try-except to finish up our age data solution, which also introduces the break and continue statement options for while loops as well.

The code

while True:
    age = input("Please enter your age (1 thru 125): ")
    try:
        age = int(age)
    except:
        print('Error: Please use only digits 0 thru 9.')
        continue
    if age < 1 or age > 125:
        print('Error: Valid age range is 1 thru 125.')
        continue
    break
print('Your age is', age, sep=" ")

Code Details:


Additional Documentation

There are additional features and options available with exception handling in Python. Full documentation is available at the Python.org documentation site: Errors and Exceptions

Practice Problems

Problem 1

Write a Python program that improves the division by zero example provided above (under the Try-Except section) to use a while True - try - except structure similar to the Age Validation Solution demonstrated above.




 





© 2023 John Gordon
Cascade Street Publishing, LLC