Year 1 Computer Programming - Lecture 5
Year 1 Computer Programming - Lecture 5
(Based on Lecture Slides Prepared by Richard Kay & Dr. Shadi Basurra)
● Introduction to debugging
● Syntax Errors
● Code inspection
● Fixing compilation errors
● Runtime Errors
● Understanding runtime exception tracebacks
● Logical Errors
● Plan, then test
● Break Points
● Watch variables
● Use of the IDLE debugger
Syntax Errors:
– Common syntax errors could be incorrect indentation, missing
elements, misspelling etc. Easiest to find, by code inspection or the
interpreter reports it at compilation time.
Runtime Errors:
– May go unnoticed at compilation time and only revealed at runtime.
Logical Error:
– Usually the hardest to find. Can be identified by observing/watching the
program behaviour.
Python interpreter uses the indentation to mark the beginning and end of
blocks.
Some of the block control key words in Python are def, for, if, else, elif, try,
except etc.
Example
1. def test():
2. for year in [2000,1903,2008,2009]:
3. if(isleap(year)):
4. isornot=""
5. else:
6. isornot="not "
7. print(str(year)+" is "+isornot+"a leap year“)
How does the interpreter know which statements are controlled by which
block controls ? It can't form a layout like this.
CMP 4266, School of CS and DT, Birmingham City University.
Code inspection: correcting block structure
1. def test():
2. for year in [2000,1903,2008,2009]:
3. if(isleap(year)):
4. isornot=""
5. else:
6. isornot="not "
7. print(str(year)+" is "+isornot+"a leap year“)
1. def test():
2. for year in [2000,1903,2008,2009]:
3. if(isleap(year)):
4. isornot=""
5. else:
6. isornot="not "
7. print(str(year)+" is "+isornot+"a leap year“)
1. def test():
2. for year in [2000,1903,2008,2009]:
3. if(isleap(year)):
4. isornot=""
5. else:
6. isornot="not "
7. print(str(year)+" is "+isornot+"a leap year“)
Brackets and quotes (i.e. () [] {} "" ' ‘) must appear as matching pair.
Block control statements def, if, for, while etc. must end with a : colon.
A backslash \ or % escape can make the following character into something
different.
List items are separated using commas (,).
1. def test( :
2. for year in [2000.1903'2008,2009 };
3. if (isleap(year):
4. isornot='"
5. else
6. isornot="not \"
7. print(str(year)+" is " &isornot+"a leap year“)
1. def test():
2. for y in [2000,1903,2008,2009]:
3. if(isleap(year)):
4. isornot=""
5. else:
6. isorno="not "
7. print(str(yaer)+" is "+issornot+"a leap year“)
8. Test()
The defined function name needs to be checked against all calls to it.
The variables used for year and isornot also need consistent spelling.
Use edit find (<ctrl><f>) and find again (<ctrl><g>) to check consistency
and to cycle through occurrences of same function, module, class or
variable name.
We still need to do a
careful visual code
inspection.
Can be handled by
careful coding and
exception handling
A break point is a source code line where we temporarily pause the running
program while we are in the process of debugging it.
If the program has done everything it should have correctly prior to the
breakpoint, the error must be after the breakpoint.
We can then set another breakpoint, and check whether the error we are
searching for occurs between the 2 breakpoints, to narrow down search
space.
IDLE has a built in debugger that allows to analyze the program's state
after each statement
IDLE debugger enable us to step into functions one source code line at a
time, step over these or out of them as needed, or just run the program
normally. We can watch the variables change as we do this.
Buttons:
– Go: Run the program until the next breakpoint
- To set breakpoints, right click on a line and choose “set breakpoint”.
– Step: Execute the next statement. If the next statement is a function call
execution will step into the function
– Over: Same as step, except if the next statement is a function call, it will
not step into the function.
– Out: complete the current function
Options:
– Stack: Displays the current running function
– Source: Displays the current statement in the source file
– Locals: Displays the local variables and their values
– Globals: Displays the global variables with their values
Test Case is the basic component to perform software testing. In the most
general form test cases
– specifies the actual input values and anticipate output values
– identifies any constrains on the execution of the test case (e.g. processor
speed, available memory)
Equivalence partitioning
– Possible inputs could be very large and infinite, so testing is limited to a small
subset of inputs.
- Divide a set of test conditions into partitions that can be considered the same.
- Then we take one value from each partition for testing. The hypothesis is, if one value of the partition pass, all
other values will also pass. Likewise, if one condition in one partition fails, all numbers in this condition will fail.
Example
– Let's consider the behaviour of tickets in the Flight reservation application, while
booking a new flight.
– Ticket values 1 to 10 are considered valid & ticket is booked. While value 11 to
99 are considered invalid for reservation and error message will appear, "Only
ten tickets may be ordered at one time."
Example
Decision/condition coverage
– every condition in a decision in the program has taken all possible
outcomes at least once
– every decision in the program has taken all possible outcomes at least
once.
TC1 View contact User to run the programme. A list of already A list of existed Passed
list Main menu to appear with 4 existing contacts will appear
options: contacts will
User to input option: v appear