User input and error handling
- PDF / 1,238,684 Bytes
- 74 Pages / 547.087 x 737.008 pts Page_size
- 105 Downloads / 198 Views
User input and error handling
Consider a program for evaluating the formula x = A sin(wt): 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 chapter starts with describing three different ways of reading data into a program: 1. let the user answer questions in a dialog in the terminal window (Section 4.1), 2. let the user provide input on the command line (Section 4.2), 3. let the user provide input data in a file (Section 4.5), 4. let the user write input data in a graphical interface (Section 4.8). Even if your program works perfectly, wrong input data from the user may cause the program to produce wrong answers or even crash. Checking H.P. Langtangen, A Primer on Scientific Programming with Python, Texts in Computational Science and Engineering 6, DOI 10.1007/978-3-642-54959-5_4, © Springer-Verlag Berlin Heidelberg 2014
147
148
4 User input and error handling
that the input data are correct is important, and Section 4.7 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. Section 4.9 tells you how easy it is to make your own modules. All the program examples from the present chapter are available in files in the src/input1 folder.
4.1 Asking questions and reading answers One of the simplest ways of getting data into a program is to ask the user a question, let the user type in an answer, and then read the text in that answer into a variable in the program. These tasks are done by calling a function with name raw_input in Python 2 - the name is just input in Python 3.
4.1.1 Reading keyboard input A simple problem involving the temperature conversion from Celsius to Fahrenheit constitutes our main example: F = 95 C + 32. The associated program with setting C explicitly in the program reads C = 22 F = 9./5*C + 32 print F
We may ask the user a question C=? and wait for the user to enter a number. The program can then read this number and store it in a variable C. These actions are performed by the statement C = raw_input(’C=? ’)
The raw_input function always returns the user input as a string object. That is, t
Data Loading...