$$ \newcommand{\tp}{\thinspace .} $$

User input and error handling

Hans Petter Langtangen [1, 2]

[1] Center for Biomedical Computing, Simula Research Laboratory
[2] Department of Informatics, University of Oslo

Jun 14, 2016


Table of contents

Asking questions and reading answers
      Reading keyboard input
Reading from the command line
      Providing input on the command line
      A variable number of command-line arguments
      More on command-line arguments
Turning user text into live objects
      The magic eval function
      The magic exec function
      Turning string expressions into functions
Option-value pairs on the command line
      Basic usage of the argparse module
      Mathematical expressions as values
Reading data from file
      Reading a file line by line
      Alternative ways of reading a file
      Reading a mixture of text and numbers
Writing data to file
      Example: Writing a table to file
      Standard input and output as file objects
      What is a file, really?
Handling errors
      Exception handling
      Raising exceptions
A glimpse of graphical user interfaces
Making modules
      Example: Interest on bank deposits
      Collecting functions in a module file
      Test block
      Verification of the module code
      Getting input data
      Doc strings in modules
      Using modules
      Distributing modules
      Making software available on the Internet
Making code for Python 2 and 3
      Basic differences between Python 2 and 3
      Turning Python 2 code into Python 3 code
Summary
      Chapter topics
      Example: Bisection root finding
Exercises
      Exercise 1: Make an interactive program
      Exercise 2: Read a number from the command line
      Exercise 3: Read a number from a file
      Exercise 4: Read and write several numbers from and to file
      Exercise 5: Use exceptions to handle wrong input
      Exercise 6: Read input from the keyboard
      Exercise 7: Read input from the command line
      Exercise 8: Try MSWord or LibreOffice to write a program
      Exercise 9: Prompt the user for input to a formula
      Exercise 10: Read parameters in a formula from the command line
      Exercise 11: Use exceptions to handle wrong input
      Exercise 12: Test validity of input data
      Exercise 13: Raise an exception in case of wrong input
      Exercise 14: Evaluate a formula for data in a file
      Exercise 15: Write a function given its test function
      Exercise 16: Compute the distance it takes to stop a car
      Exercise 17: Look up calendar functionality
      Exercise 18: Use the StringFunction tool
      Exercise 19: Why we test for specific exception types
      Exercise 20: Make a complete module
      Exercise 21: Organize a previous program as a module
      Exercise 22: Read options and values from the command line
      Exercise 23: Check if mathematical identities hold
      Exercise 24: Compute probabilities with the binomial distribution
      Exercise 25: Compute probabilities with the Poisson distribution
References

Consider a program for evaluating the formula \( x = A\sin(w t) \):

from math import sin
A = 0.1
w = 1
t = 0.6
x = A*sin(w*t)
print x
In this program, A, w, and t are input data in the sense that these parameters must be known before the program can perform the calculation of x. The results produced by the program, here x, constitute the output data.

Input data can be hardcoded in the program as we do above. That is, we explicitly set variables to specific values: A=0.1, w=1, t=0.6. This programming style may be suitable for small programs. In general, however, it is considered good practice to let a user of the program provide input data when the program is running. There is then no need to modify the program itself when a new set of input data is to be explored. This is an important feature, because a golden rule of programming is that modification of the source code always represents a danger of introducing new errors by accident.

This document starts with describing four different ways of reading data into a program:

  1. let the user answer questions in a dialog in the terminal window (the section Asking questions and reading answers),
  2. let the user provide input on the command line (the section Reading from the command line),
  3. let the user provide input data in a file (the section Reading data from file),
  4. let the user write input data in a graphical interface (the section A glimpse of graphical user interfaces).
Even if your program works perfectly, wrong input data from the user may cause the program to produce wrong answers or even crash. Checking that the input data are correct is important, and the section Handling errors tells you how to do this with so-called exceptions.

The Python programming environment is organized as a big collection of modules. Organizing your own Python software in terms of modules is therefore a natural and wise thing to do. The section Making modules tells you how easy it is to make your own modules.

All the program examples from the present document are available in files in the src/input folder.

This chapter is taken from the book A Primer on Scientific Programming with Python by H. P. Langtangen, 5th edition, Springer, 2016.