← Back to Book Detail

7 Keyboard Input and Printed Output (7/24) -- Rick's Measurement for Mechatronics Note...

Browse
29%

7 Keyboard Input and Printed Output

7 Keyboard Input and Printed Output For most simple programs it will be enough to read information in from the standard input device (e.g. a keyboard) and write information out to the standard output device (e.g. a terminal screen). We will focus on those capabilities in ways that make it easy to extend your understanding to reading and writing files on storage media. Printed Output The simplest programs have input values built right into the code by assigning variables, so we will start with output, focussing on three types of data, to be written out as human readable characters. - String constants are enclosed in quotes and used simply to label the rest of your output. String or character variables can be created in your programs, however we will focus on numbers, - integers, unsigned integers and - floating point numbers with decimals. print() , or something with a similar name, is the usual function called, reflecting history of mechanically printing characters on paper, either one at a time, or line by line. Thinking of that step-wise mechanical process may help you visualize how to print more complicated messages in simple steps. Python print(i,j,x) will print all three variables, leaving spaces in between and adding a line break at the end of printing, just as you might expect. Floating point values will be printed with many decimal places, usually more than you wanted to see. Formatting your output will make it more readable. print("A format string for unsigned %5u, int %7d, and double %8.3f"%(i,j,x)); print("A format string for unsigned {0:5d}," " int {1:7d}, and double {2:8.3f}".format(i,j,x)); are two different ways to accomplish the same thing in Python. The first one is deprecated (meaning it still works, but may not in the future) and descends from C. The second one is the preferred approach going forward. Recognize the first one when you see it, but try to use the second one if you want your code to keep working in the long term. {0:5d} tellsformat() to use the first argument (i index value zero) and format it as an integer 5 or more characters long, padding the start with spaces if needed.{1:7d} formats the second argument as an integer 7 or more characters long. Note that you can break a string across lines for readability by adding two quotation marks.{2:8.3f} fills in the string with the third (2) argument formatted with 3 decimal places in a slot at least 8 characters long. Try it yourself by starting with the Printing and Formatting Data notebook in the Python Learning Sequence. C printf("A format string for unsigned %5u, int %7d, and double %8.3f\n",i,j,x); will print the text string inside the quotation marks, filling in the three numeric variables at the locations with the % formatting elements %5u tellsprintf() to expect something thesizeof() an unsigned variable and to format it as a range from 0 to a large positive value, using leading spaces so as to print at least 5 characters.%7d is similar, except it interprets the
← Previous Chapter Next Chapter →