CSC 226

CSC 226 logo

Software Design and Implementation


A1: Breaking Bad-- On coding errors

Objectives

  • 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.

  1. Open Spyder, if you are in Windows, you should see Anadonda in your program list, and Spyder will be in a submenu.

    Spyder menu

    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.

    Spyder splash screen

  2. Download the file choc.py. Download choc.py and save it in a folder where you plan to keep code for this course.

  3. To open it in Spyder, you can use File: Open
    Spyder 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.
  4. Run the program a few times, using the big green arrow or the f5 key
    Spyder run
  5. 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.
    iPython interaction
    You can type right in this iPython console window.
You will be making changes to the code in order to introduce errors in the source code to see what happens.

Fundamentals of Programming Errors (bugs)

There are many kinds of programming errors or bugs.
  • Asyntax 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 top by the variable bottom. The syntax may be correct, but the program will terminate early (commonly called crash) ifbottom has 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 Log

This 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 Task

This 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:

  1. Change: From this web page, copy the number and description of the change you were asked to make into yourerror log file. After making the specified change in the program. Then if possible, try to run the changed program.
  2. 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 correct.
    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.
  3. 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.
  4. 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.
Example:
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 0:
  1. Change: Remove the second quotation mark from the line: entered_name = raw_input("Please enter your name: ")
  2. Classification: Syntax Error because it is a grammatical error.
  3. Error Indication(s):
    In the code window, on the line where the error was created, a yellow warning symbol appeared. Hovering over this symbol gives, "Code Analysis: SyntaxError: EOL while scanning string literal".
    Spyder Code Analysis
    In the console window, a ^ caret appears immediately following the ")". This is followed by the statement, "SyntaxError: EOL while scanning string literal"
  4. Correspondence: Excellent. The problem we created was that the string never ended. The warning symbol appeared on thecorrect line and the caret appeared just after where the problem was.

Changes

  1. Change the line oz_per_lb = 16 to 16 = oz_per_lb
  2. Remove the int from the line: num_lbs = int(raw_input("How many lbs of chocolate in a box? "))
  3. Change both occurrences of oz_per_lb to 16oz_per_lb
  4. Change the line print("Hello "+ entered_name+"! \n") to print("Hello "+ entered_name+"! /n")
  5. Change the line if oz_choc > 500: by changing the > to a <.
  6. Remove the colon from the line if oz_choc > 500:
  7. Change the algorithm by changing the line oz_choc = lbs_choc * oz_per_lb to oz_choc = lbs_choc / oz_per_lb
  8. Deliberately create a syntax error which differs from all of the above. Describe how to create the error and then do parts a-d.
  9. 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.
  10. 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.
Include the following header at the top of your error log:
 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
@author: pearcej
: Jan Pearce

Save this file as yourusername-A1.doc or yourusername-A1.docx.

Submit yourusername-A1.docx to theA1 dropbox in Moodle before our next class.

Copyright 2016 | http://cs.berea.edu/courses/CSC226/ | Licensed under a Creative Commons Attribution-Share Alike 3.0 United States License