Software Design and Implementation
A1: Breaking Bad-- On coding errors
- Explore the Spyder the integrated Development Environment (IDE)
- Practice editing, building, and running Python programs.
- Learn to interpret error messages from the Spyder IDE in order to help to debug your computer program.
- Learn the rules for naming identifiers in Python
- Learn to keep an error log.
Be sure to read chapters 1 and 2 of the text before beginning this assignment.
Some Background and Terminology
To create a computer program, one needs an editor to type what is called the source code of the program. The source code contains the sequence of steps the programmer designed for the program (the algorithm) written in a specific language. The computer cannot yet execute this code at this stage because it is written in a high-level language designed for human readability, and it must be translated for the computer to understand it. This translation happens via a IDE or an interpreter.
We must have some way to check what happens when our program fails to work correctly, due to a programming error called a bug. According to folklore, the first computer "bug" was an actual moth discovered by Grace Hopper that was trapped between two electrical relays of the Mark II Aiken Relay Calculator and caused the machine to crash.A "bug" is a term used to describe a situation where a program, for a variety of reasons does not do what it is expected to do. Some bugs are found by the IDE, but others require a utility program called a debugger to find and correct these mistakes. Debuggers enable programmers to stop running their program at any point and examine the contents of any of the variables to ensure that their values are what they are expected to be.
Open Spyder, if you are in Windows, you should see Anadonda in your program list, and Spyder will be in a submenu.
You should see the splash screen appear while it is opening. Note that sometimes it will appear behind other windows if you have other programs open.
- Download the file
choc.py. Download choc.py
and save it in a folder where you plan to keep code for this
- To open it in Spyder, you can
use File: Open
or if Spyder is open, you can drag the file over the editing window in Spyder and drop it.
Note that double-clicking a python file will not open Spyder--you will need to open the program from within Spyder.
- Run the program a few times,
using the big green arrow or the f5 key
- Note how the interaction appears
in the iPython concole window in the lower right corner of the
screen. Enter data in the iPython console window when requested.
You can type right in this iPython console window.
Fundamentals of Programming Errors (bugs)There are many kinds of programming errors or bugs.
- A syntax error is an error in the grammar, structure, or order of the elements in the source code. Syntax errors are typically detected by the IDE and flagged with a red squiggly underline in Spyder.
- Run-time errors are generally
detected by the IDE, but are only detected when the user attempts
to run the program. One standard example of a run-time error is
where the source code has an instruction to divide the variable
topby the variable
bottom. The syntax may be correct, but the program will terminate early (commonly called crash) if
bottomhas the value of 0. Usually run-time errors will cause the running program to crash, but they sometimes just generate warnings during the program execution.
- Semantic errors or logic errors are the most subtle, where the program is coded correctly and does not crash but do not perform the right operation to produce the expected results. Errors of this kind cannot be found automatically by the IDE or easy to determine by the program's output. They are usually due to errors in the design or how the algorithm was translated into the source code.
Creating an Error LogThis assignment is designed for you to become more familiar with the error detection built into the Spyder IDE. In particular, you will create a catalog of typical errors that may arise, and this document may hopefully help you in the future to identify the actual problem based upon the error message the IDE produces.
Open a new Microsoft Word or Open Office document and name your file yourusername-A1.docx. This will be your error log file.
Your TaskThis assignment must be completed individually.
Before our next class, you will be submitting an error log entitled yourusername-A1.doc or yourusername-A1.docx
Making an error log
To begin the error log open a new Word document entitled yourusername-A1.doc or yourusername-A1.docx
For each of the numbered changes below, do all of the following:
- Change: From this web page, copy the number and description of the change you were asked to make into your error log file. After making the specified change in the program. Then if possible, try to run the changed program.
- Classification: Some
of the listed changes may not generate any errors, but most will.
Spyder will try to classify it for you, but it may or may not be
Classify these changes as follows: If an error or warning occurs, classify the FIRST error or warning identified as SYNTAX , RUN TIME, or SEMANTIC/LOGIC with a very brief explanation. Otherwise, make your classification NONE for no errors. Remember that just because the IDE doesn't catch it, does not mean there is not an error.
- Error Indication: Note
the FIRST error indication or message if one appears. Then copy or
describe the resultant error and/or warning message you got into
your Word error log file.
- Correspondence: Briefly
explain the correspondence between the change that you made and
the resulting error or warning and FIRST flagged message that
occurred. In other words, did Spyder identify the actual problem?
If not, can you briefly explain why?
Finally, repair each change that you just made to restore the program to its original state. Thus, after making each of the charges, you will begin again with the original working code.
Hint: The undo command is often useful here.
0. Remove the second quotation mark from the line: entered_name = raw_input("Please enter your name: ")
The entry in your error log will appear as follows:
- Change the line
oz_per_lb = 16to
16 = oz_per_lb
- Remove the
intfrom the line:
num_lbs = int(raw_input("How many lbs of chocolate in a box? "))
- Change both occurrences of
- Change the line
print("Hello "+ entered_name+"! \n")to
print("Hello "+ entered_name+"! /n")
- Change the line
if oz_choc > 500:by changing the
- Remove the colon from the line
if oz_choc > 500:
- Change the algorithm by changing the line
oz_choc = lbs_choc * oz_per_lbto
oz_choc = lbs_choc / oz_per_lb
- Deliberately create a syntax error which differs from all of the above. Describe how to create the error and then do parts a-d.
- Deliberately create a semantic/logic error which differs from all of the above. Describe how to create the error and then do parts a-d.
- Deliberately create a run-time error which differs from all of the above. Describe how to create the error and then do parts a-d.
CSC 226: Software Design and Implementation
Assignment A1: Developing and Error Log
Purpose: To learn to use the IDE and to interpret error messages from the IDE
: Jan Pearce
Save this file as yourusername-A1.doc or yourusername-A1.docx.Submit yourusername-A1.docx to the A1 dropbox in Moodle before our next class.