PHYCS 3730/6720 Lab Exercise

Reading and references:
Answer file Mylab14.txt.
Here we learn more about input and output in Python -- specifically, how to read and write text files and how to use the standard error stream.

Exercise 1. Checking for EOF on standard input

Sometimes it is useful to read a set of values from standard input even if you don't know how many there are. To do this, you need a way to stop reading when an end-of-file is reached. The notes for this class period shows how to do that.

Write code for reading the components of a vector of unspecified length (number of components) from the keyboard, one value per line, without prompts. Stop when the end of file is reached. (If you aren't sure how to check for an end of file, see the notes for this class.) (Remember, Control-d signals a keyboard end of file.) Your code should put the components in a vector v (Make it a Python list, not a numpy vector). The Python append() method adds to the end of a vector (list). So if you have a float x, then v.append(x) appends x to the end of the list. When all the data has been read, have your program print the resulting vector.

Copy your code to the answer file. Test it by feeding it input from the screen. Then (using emacs) creata file called data, put numbers in it, and try reading it using input redirection with <).

Exercise 2. Opening and writing to a file

Modify your code from Lab 13, Exercise 1, so it writes the table to a file called CtoF.txt. Copy your modified code to the answer file.

Exercise 3. Reading from a specific file

Modify the code you created in Exercise 1 above so you open and read from the file data up to the end-of-file.. Make sure you take care of the conversion from a string to a float. Include code for catching bad input.

Copy your modified code to the answer file.