Debugging CMSC 201. Announcements Hw2, Hw3 grades returned if something doesn’t seem right, ask Midterm next Thurs (Oct 23) we’ll review on Tues no hw.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 7: Errors 1 Based on slides created by Bjarne Stroustrup.
Debugging Introduction to Computing Science and Programming I.
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
Complexity (Running Time)
Chapter 2 Writing Simple Programs
Group practice in problem design and problem solving
CSE 486/586 CSE 486/586 Distributed Systems PA Best Practices Steve Ko Computer Sciences and Engineering University at Buffalo.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
General Programming Introduction to Computing Science and Programming I.
COMPSCI 101 Principles of Programming Lecture 28 – Docstrings & Doctests.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
C++ crash course Class 8 statements, sort, flight times program.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 5: Structured Programming
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
Announcements: Project 5 Everything we have been learning thus far will enable us to solve interesting problems Project 5 will focus on applying the skills.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2.
Error Handling Tonga Institute of Higher Education.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Head First Python: Ch 3. Files and Exceptions: Dealing with Errors Aug 26, 2013 Kyung-Bin Lim.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CSC 108H: Introduction to Computer Programming
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
FOP: Multi-Screen Apps
Discussion 4 eecs 183 Hannah Westra.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Introduction to Computing Science and Programming I
Week 9 - Monday CS 113.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
What to do when a test fails
Testing and Debugging.
Topic: Functions – Part 2
Topics Introduction to Repetition Structures
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
CSS 161: Fundamentals of Computing
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Cracking the Coding Interview
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Last Class We Covered Data representation Binary numbers ASCII values
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Testing UW CSE 160 Winter 2016.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Python programming exercise
15-110: Principles of Computing
CMSC201 Computer Science I for Majors Final Exam Information
Topics Introduction to Repetition Structures
Arrays.
Software Development Techniques
Presentation transcript:

Debugging CMSC 201

Announcements Hw2, Hw3 grades returned if something doesn’t seem right, ask Midterm next Thurs (Oct 23) we’ll review on Tues no hw next week still have lab Questions?

Debugging Debugging is the process of removing errors from our code. It, like anything else in computer science, takes practice.

Errors There are three kinds of errors you’ll run into: Syntax errors—These are errors that occur from poorly formed python syntax. Exceptions—Exceptions occur when you try and do something that python can’t let you do, like access an element of a list that doesn’t exist. Logic Errors—Logic errors occur when your program terminates successfully, but just doesn’t do the right thing.

Syntax Errors These are the easiest kind of error to debug! 1. Look at what python is telling you the problem is. 2. Go to the line python is telling you the problem is on. 3. Fix the part of your program that is incorrect python. In some cases, you might have to look at your indentation, or the line above!

Exceptions Possible reasons for exceptions: File not found List index out of bounds Expected an integer but got a string Fixing these is also not a big deal! Python will tell you exactly what the error is, and what line it happened on. If a quick inspection of that line does not help you figure out what’s wrong, you might want to move on to the tricks we talk about for logic errors.

Logic Errors Logic errors are when your program simply does the wrong thing. These are the hardest to fix. Typically, with a logic error, all you know is that your program is producing incorrect output. The trick is to isolate where the error is happening.

Why We Have Functions When we’re trying to hunt down an error, functions are a helpful tool for this. Because functions take a small number of arguments and return a value, it’s easy to isolate them from the rest of the program to see if they’re working or not. def someCrazyFunction(input1, input2, input3): #All sorts of code return blah In order to see if our error is in this function, we can simply run it in main with a variety of different inputs and verify if it works or not.

How To Test A Function Imagine we have a sort function. What possible ways can we test it? sort([1, 10, 4, 5, 2]) Pretty standard! We know what the output should be. This is a general test case.

How To Test A Function Depending on how our program’s using it though, there could be other, weird cases! sort([]) sort([2]) These are called edge cases—the program should work, but it wasn’t necessarily what you had in mind.

How To Test A Function Notice that we can also have cases like these: sort([ “hats”, 1]) sort(10) However, these aren’t really worth testing! They will always break our sort function, and should! They violate what’s called the function’s precondition.

Precondtion A precondition is something that must be true about a function’s inputs in order for it to do its job. For sort(), an example would be “It must get a list of things that can all be compared to each other.” For a square root function, the precondition could be “Input must be a positive number.” Preconditions should be described in the function header comment.

Testing Functions So for functions, we want to test several general cases and several edge cases, and make sure that if something violates it’s precondition it reacts appropriately. Once a function has been thoroughly tested, you know that function’s not going to be a problem anymore!

Finding Errors Eventually, you’ll get to a function that is not producing the correct output, and your new job is to isolate the error within that function.

Example def longestSubSeq(myList): lastItem = myList[0] current = 0 best = 0 for item in myList: if(item == lastItem): best = best + 1 if best < current: best = current else: lastItem = item current = 1 return best What are some good test cases?

Test Cases def main(): print(longestSubSeq([1]) print(longestSubSeq([1, 2, 3]) print(longestSubSeq([1, 1, 1, 2]) print(longestSubSeq([1, 1, 2, 1]) print(longestSubSeq([1, 1, 2, 3, 2, 2, 2, 2]) Each of these targets a different place where something could go wrong!

Test Cases def main(): print(longestSubSeq([1]) print(longestSubSeq([1, 2, 3]) print(longestSubSeq([1, 1, 1, 2]) print(longestSubSeq([1, 1, 2, 1]) print(longestSubSeq([1, 1, 2, 3, 2, 2, 2, 2]) Output: File "oct14.py", line 17 print(longestSubSeq([1, 2, 3]) ^ SyntaxError: invalid syntax

Test Cases def main(): print(longestSubSeq([1])) print(longestSubSeq([1, 2, 3])) print(longestSubSeq([1, 1, 1, 2])) print(longestSubSeq([1, 1, 2, 1])) print(longestSubSeq([1, 1, 2, 3, 2, 2, 2, 2])) Output:

Test Cases We know something’s wrong! How do we find it?

Strategies Tracing by hand can be effective. Take the input, pretend to be the computer, and try and run the program yourself. Pros: Gives you a good idea of what your program is doing. Can help find simple errors. Cons: Can take awhile, especially for things with loops. Can be impractical for large inputs.

Strategies Print statements. Pros: Very informative, you can target specific variables to print. Cons: Have to know what to look at. Need careful labeling, or they get very confusing.

Example def longestSubSeq(myList): lastItem = myList[0] current = 0 best = 0 for item in myList: if(item == lastItem): best = best + 1 if best < current: best = current else: lastItem = item current = 1 return best Where are some good places for print statements?

WRONG def longestSubSeq(myList): lastItem = myList[0] current = 0 best = 0 for item in myList: print(item) if(item == lastItem): print(“hello”) best = best + 1 if best < current: best = current print(“here”) else: lastItem = item current = 1 print(“doing thing”) return best

Example def longestSubSeq(myList): lastItem = myList[0] current = 0 best = 0 for item in myList: print(“The current item is: “ + str(item) if(item == lastItem): print(“Item is part of sequence”) best = best + 1 if best < current: best = current print(“Longest sequence so far”) else: lastItem = item current = 1 print(“Sequence broken”) return best

Example Looking at the output, we can see right away that we never make it to longest sequence so far! That means best is never less than current… Looks like we forgot to increment current!

Example Still doesn’t fix it. Let’s try printing more stuff… def longestSubSeq(myList): lastItem = myList[0] current = 0 best = 0 for item in myList: print(“The current item is: “ + str(item)) if(item == lastItem): print(“Item is part of sequence”) print(“Best is “ + str(best)) best = best + 1 current = current + 1 if best < current: best = current print(“Longest sequence so far”) else: lastItem = item current = 1 print(“Sequence broken”) return best

Example Still doesn’t fix it. Let’s try printing more stuff… def longestSubSeq(myList): lastItem = myList[0] current = 0 best = 0 for item in myList: print(“The current item is: “ + str(item)) if(item == lastItem): print(“Item is part of sequence”) print(“Best is “ + str(best)) best = best + 1 current = current + 1 if best < current: best = current print(“Longest sequence so far”) else: lastItem = item current = 1 print(“Sequence broken”) return best

Example def main(): print(longestSubSeq([1])) print(longestSubSeq([1, 2, 3])) print(longestSubSeq([1, 1, 1, 2])) print(longestSubSeq([1, 1, 2, 1])) print(longestSubSeq([1, 1, 2, 3, 2, 2, 2, 2])) Output:

Example Debugging poorly will be your single biggest time sink in this class! Isolate the bug to a function or block of code, and put in well designed print statements at the start and end of the broken section, and narrow it down.

Exercise # Merges two sorted lists. # Precondition: list1 and list2 are lists of integers already sorted in ascending # order (smallest to largest) def merge(list1, list2): current1 = 0 current2 = 0 result = [] while current1 < len(list1) and current2 < len(list2): if(current1 == len(list1)): result.append(list2[current2]) current2 = current2 + 1 elif(current2 == len(list2)): result.append(list2[current1]) current1 = current1 + 1 elif(list1[current1] < list2[current2]): result.append(list2[current2]) current2 = current2 + 1 else: result.append(list1[current1]) current1 = current1 + 1 return result

Exercise def main(): main() Write some test cases, and then try debugging!