This handout shows what we expect for the “refine” file and the “journal” file.
This file, called “refine.txt” (if it is a text file) or “refine.pdf” (if it is a PDF file), contains the way you go from the problem statement to the program.
Let's say the problem is:
We’ll refine this in two steps.
Here, we just write down the steps needed to solve the problem.
First refinement:
1. Read in a temperature
2. Apply the formula F = (9/5)C + 32
3. Print the result
Now, we break each step down into smaller steps where possible.
First refinement:
1. Read in a temperature
2. Apply the formula F = (9/5)C + 32
3. Print the result
Putting this together:
Second refinement:
1a. Read in a string
1b. Convert the string to a floating-point number
1c. Store the number in C
2a. Apply the formula F = (9/5)C + 32 and store the result in F
2b. Store the result in F
3. Print F
So, your refinement file would look like this:
First refinement:
1. Read in a temperature
2. Apply the formula F = (9/5)C + 32
3. Print the result
Second refinement:
1a. Read in a string
1b. Convert the string to a floating-point number
1c. Store the number in C
2a. Apply the formula F = (9/5)C + 32 and store the result in F
2b. Store the result in F
3. Print F
Don’t include the final program—but just to show you the final refinement, here is the program, with comments showing which steps of the second refinement correspond to the lines of the program:
temp = input("What Celsius temperature should I convert? ") # corresponds to 1a
C = float(temp) # corresponds to 1b and 1c
F = (9/5) * C + 32 # corresponds to 2a and 2b
print(C, "Celsius is", F, "Fahrenheit") # corresponds to 3
The journal of errors is a file that contains non-syntax errors, and how you fixed them. The errors that go her are ones that occur when your program runs—if you leave off a parenthesis and the Python interpreter (IDLE or PyScripter) says “Incorrect syntax” or “Syntax error”, please don’t list the error in this journal. But if the program runs, and gives an error message or incorrect results when you test it, then this goes into your journal of errors.
Here’s an example. Suppose we do the above refinement, forget step 1b, and make a mistake in the formula when we write our program. So it looks like this:
C = input("What Celsius temperature should I convert? ")
F = (9/5) * C - 32
print(C, "Celsius is", F, "Fahrenheit")
When you first run this program, it asks you for a Celsius temperature (I typed “0”). It then prints:
Traceback (most recent call last):
File "/Users/bishop/Documents/tempcvtD.py", line 2, in <module>
F = (9/5) * C - 32
TypeError: can't multiply sequence by non-int of type 'float'
This means that the variable C is not a float. Looking back, I see I forgot to convert the input string to a float. So my entry in the journal would look like:
First try:
Got a "TypeError: can't multiply sequence by non-int of type 'float'"
Caused by not converting result of input to a float
Fixed by using "float" function
So, I add the appropriate conversion and run the program. I don’t get any error message, but the output tells me 0 Celsius is -32 Fahrenheit, which is incorrect (it’s actually 32 Fahrenheit). Looking at the program, I check that I translated the formula correctly, and see that I subtracted when I should have added. So, my entry in the journal for this would be:
Second try:
0 Celsius came out as -32 Fahrenheit, should be 32
Formula wrong; should be (9/5)*C+32, not (9/5)C-32
Now the program works. So the final journal of errors contains:
First try:
Got a "TypeError: can't multiply sequence by non-int of type 'float'"
Caused by not converting result of input to a float
Fixed by using "float" function
Second try:
0 Celsius came out as -32 Fahrenheit, should be 32
Formula wrong; should be (9/5)*C+32, not (9/5)C-32
ECS 10, Basic Concepts of Computing Winter Quarter 2012 |