A202/I211 Assignment 6
Functions and File I/O
Due Thursday, October 14, 3:00 PM
Individual work this week.
In lab
Recall from class the STAIR steps to problem solving:
In a file named lab6.py define a procedure, named
convertFC that takes as its argument the degrees in fahrenheit,
converts it to degrees celsius, and returns this new value.
>>>
convertFC(80)
26.666666666666668
>>> convertFC(32)
0.0
The calculation from fahrenheit to celsius is
C = (5/9)(F - 32) where F = degrees fahrenheit and C = degrees celsius
Hint: Remember the difference
between integer division and float division.
convertFC is working properly, the
next step is to create your forecastWeather(fileName) procedure.
This procedure will need to do the following items:
"Today it will be {temp entered by user} degrees fahrenheit."
"That is {temp converted to celsius} degrees celsius."
Your celsius temperature should be displayed with two numbers after the decimal position. For example, "That is 34.50 degrees celsius." Based on the temperature entered by the user the text will be different:
if the temp is less than 0 then write: "Wear a heavy coat."
otherwise, if the temp is less than 50 then write: "Wear a jacket."
otherwise, if the temp is greater than 100 then write: "Stay in an air conditioned room."
lastly, if the temp has made it to this point then write:"Ah just right!"
As an example, if the user enters 80 then the program should write the following to the file: Today it will be 80 degrees fahrenheit. That is 36.67 degrees celsius.
Ah, just right!
print statements informing the user of these events. The
program will print the following statements: "writing to file {fileName}..." {program writes to the file} "done" In
other words, right before your program writes the information to the file,
it will print the first statement, next the program writes the text to the
file, and lastly the program prints the "done" statement. Note: If
you are having difficulties with a lab or an assignment then using
statements like this will be very helpful for determining what is going on.
readWeatherForecast(fileName). That takes the
file name and prints the contents of the weather file. The
procedure should raise an error if the file name is not valid. The error
message raise will look like the following:main procedure
to call the correct procedure based on information entered from the command
line. The format for running this program is the following:python lab6.py forecast-weather
file forecastWeather procedure. The program
will then take the "file" file name that will be used as the fileName
parameter for the forecastWeather procedure. For example,
python lab6.py
forecast-weather todays-weather.txt python lab6.py read-weather
file readWeatherForecast procedure. The
program will then take the "file" file name that will be used as the fileName
parameter for the readWeatherForecast procedure. For example,
python lab6.py
read-weather todays-weather.txt main procedure to
make sure the user has entered in the correct number of system arguments. In
both usages of our program, the user must have three system arguments (the
lab6.py file name, the read/forecast argument --think of this as an operation
argument--, and the filename argument). Validate that the number of system
arguments is correct. We also need to validate that the read/forecast argument
is correct. The value can either be "forecast-weather" or "read-forecast". If
the program encounters any other value then it should print "Error: invalid
operation" M:\lab6>python lab6.py forecast-weather
Usage: python l6.py forecast-weather file
python l6.py read-weather file M:\lab6>python lab6.py forecast-weather weather.txt What temperature in degrees fahrenheit will it be today? 50 writing to file weather.txt ...done M:\lab6>python lab6.py read-weather Usage: python lab6.py forecast-weather file python lab6.py read-weather file M:\lab6>python lab6.py read-weather weather.txt Today it will be 50 degrees fahrenheit. That is 10.00 degrees celsius. Ah just right!Hints:
print statement to print the temperature messages
before writing to the file. Once you have this working then you know the
information you will write the file will be valid.
readWeatherForecast procedure can should be able to
read information from any file. Therefore, you can test this on any file you
want. When you have completed the last exercise above, or 15 minutes before the lab ends, whichever comes first, submit your lab6.py file as lab 6 in Vincent.
>>> reverseString("abc")
'cba'
>>>
reverseString(5)
Traceback (most recent call last):
File
"<stdin>", line 1, in ?
File "reverse.py", line 14, in
reverseString
raise 'Not a string: ' + str(plainString)
Not a string:
5
Hint: Use a for loop.
>>> reverseWordChars("abc def")
'cba fed'
>>> reverseWordChars("I went to the store.")
'I
tnew ot eht .erots'
Hint: While using nested for loops
will work, making a call to your reverseString method could greatly
reduce the difficulty of this method.
In the same file, create a method reverseWordOrder. This method also takes in a string as a parameter and returns a string in which the order of the words are reversed. This method should display an appropriate error message when the input argument is not a string. Within the IDLE this method should work as follows:
>>> reverseWordOrder("abc def")
'def abc'
>>> reverseWordOrder("I went to the
store.")
'store. the to went I'
Hint: There is a
simple solution that uses both the reverseString and
reverseWordChars methods. It does not even need a for loop!
print “calling sqrt: ”,
sqrt(4)
print “done”The applications arguments are a <mode> and two file names. The two file names are an input file followed by an output file. The input file should be read into a string that will be passed to one of your functions. Use read() instead of readline(). The <mode> is either text, word_letters, or word_order.
If the mode is text, your method reverseString should be called with the string read in from the input file.
If the mode is word_letters, your method reverseWordChars should be called with the string read in from the input file.
If the mode is word_order, your method reverseWordOrder should be called with the string read in from the input file.The returned result of whichever method was called should be both written to the output file and printed on the screen.
In the following sample that is run from the command line, input.txt is the input file, and output.txt is the output file, and input.txt contains: I went to the store
M:\lab6>python reverse.py text input.txt output.txt
Checking access of input.txt... done
Checking access of output.txt... done
Opening input.txt... done
Opening output.txt... done
Reversing Text in input.txt... done
Writing to output.txt... done
Closing input.txt... done
Closing output.txt... done
Output:
erots eht ot tnew I M:\lab6>python reverse.py word_letters input.txt output.txt
Checking access of input.txt... done
Checking access of output.txt... done
Opening input.txt... done
Opening output.txt... done
Reversing Word letters in input.txt... done
Writing to output.txt... done
Closing input.txt... done
Closing output.txt... done
Output:
I tnew ot eht erots M:\lab6>python reverse.py word_order input.txt output.txt
Checking access of input.txt... done
Checking access of output.txt... done
Opening input.txt... done
Opening output.txt... done
Reversing Word Order in input.txt... done
Writing to output.txt... done
Closing input.txt... done
Closing output.txt... done
Output:
store the to went INote: The diagnostic messages are formatted with a column width of 50, left aligned, before "done" is printed so that all of the “done” messages line up.
Appropriate error messages should be displayed when any of the following conditions are violated:
- The main function should take exactly 3 arguments from the command line.
- The input file and output file exist and are readable and writable respectively.
- The mode must be either text, word_letters, or word_order.
M:\lab6>python reverse.py input.txt output.txt Usage: python reverse.py <mode> inFile outFile where <mode> is either text, word_letters, or word_order Performs various operations on the characters and words in a file.
M:\lab6>python reverse.py text testone.txt output.txt Checking access of testone.txt... Error Error: testone.txt is not a readable file
M:\lab6>python reverse.py reverse_the_words input.txt output.txt Checking access of input.txt... done Checking access of output.txt... done Opening input.txt... done Opening output.txt... done Error: reverse_the_words is an invalid mode
Examples of what not to do
When you are done, submit your final reverse.py file as a6 using Vincent.