DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Similar documents
CMSC 201 Fall 2016 Lab 09 Advanced Debugging

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Getting Started with Python

Fundamentals of Programming (Python) Getting Started with Programming

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

SCHEME AND CALCULATOR 5b

Text Input and Conditionals

Python for Informatics

Flow Control. So Far: Writing simple statements that get executed one after another.

>>> * *(25**0.16) *10*(25**0.16)

Python for Non-programmers

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Lecture 4. Defining Functions

Lists, loops and decisions

61A Lecture 2. Friday, August 28, 2015

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Chapter 9: Dealing with Errors

Algorithms and Programming I. Lecture#12 Spring 2015

Decision structures. A more complex decision structure is an if-else-statement: if <condition>: <body1> else: <body2>

COMP 204: Sets, Commenting & Exceptions

Introduction to Python

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

MEIN 50010: Python Flow Control

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Python Intro GIS Week 1. Jake K. Carr

ENGR 101 Engineering Design Workshop

Python and Bioinformatics. Pierre Parutto

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Decisions, Decisions. Testing, testing C H A P T E R 7

Java+- Language Reference Manual

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Ch. 12: Operator Overloading

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Programming for Engineers in Python. Autumn

G Programming Languages - Fall 2012

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Only to be used for arranged hours. Order of Operations

Programming for Engineers in Python. Recitation 1

COMP 204: Sets, Commenting & Exceptions

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

1 Lexical Considerations

Control Structures 1 / 17

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

ECE 122 Engineering Problem Solving with Java

Debugging & Errors Why you were up till 2AM

Declaration and Memory

1007 Imperative Programming Part II

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

QUIZ: What value is stored in a after this

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Part III Appendices 165

ENGR 102 Engineering Lab I - Computation

Quiz 1: Functions and Procedures

CS Introduction to Data Structures How to Parse Arithmetic Expressions

Starting. Read: Chapter 1, Appendix B from textbook.

Variables, expressions and statements

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Intro. Scheme Basics. scm> 5 5. scm>

Here n is a variable name. The value of that variable is 176.

Functions and Decomposition

PieNum Language Reference Manual

Types, lists & functions

Simple Java Programming Constructs 4

CHIL CSS HTML Integrated Language

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

Exceptions & a Taste of Declarative Programming in SQL

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

CS 115 Lecture 8. Selection: the if statement. Neil Moore

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

Operators. Java operators are classified into three categories:

Computer Components. Software{ User Programs. Operating System. Hardware

Basics of Java Programming

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Lecture 9: July 14, How to Think About Debugging

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

5. Control Statements

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Lexical Considerations

Exceptions CS GMU

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Compiler Errors. Flash CS4 Professional ActionScript 3.0 Language Reference. 1 of 18 9/6/2010 9:40 PM

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

61A Lecture 25. Friday, October 28

Getting Started Values, Expressions, and Statements CS GMU

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Transcription:

DEBUGGING TIPS COMPUTER SCIENCE 61A 1 Introduction Every time a function is called, Python creates what is called a stack frame for that specific function to hold local variables and other information. Of course, functions will also make other function calls, which requires the creation of even more frames. So how are these frames organized? Python uses what is called a stack to hold frames. Frames are stacked in the order that they are created. Every time you make a function call, you place a frame on the stack. You can only remove that frame when both of the following conditions are satisfied: All of the frames above it have been removed; in other words, the frame we want to remove is on the top of the stack. The function that the frame belongs to has terminated. If the most recent function call raises an error (also known as an exception), Python will dump the stack which you, the programmer, will see as a traceback message. 1.1 Traceback The traceback message displays the state of the stack at the time the error was raised. Essentially, the traceback will show you the chain of function calls that caused your error. You can follow this chain to identify exactly which functions are the ones causing trouble. For example, the following Python code will output a traceback message: >>> def bug(f, x):... return f(x) >>> bug(3, 4) Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> bug(3, 4) 1

File "<pyshell#28>", line 2, in bug return f(x) TypeError: int object is not callable Notice that the lines in the traceback appear to be paired together. The first line in such a pair has the following format File "<File name>", line <number>, in <function> That line provides you with the following information: Function: The name of the function with which the frame is associated. Number: The line number in the file where the function is defined. This saves you time trying to search through your code to find the buggy line. File Name: The name of the file where the function was written. This is especially helpful when writing large programs. The second line in the pair, which is indented further in than the first line, displays the actual line of code that makes the next function call. This gives you a quick look at what arguments were passed into the function and in what context the function was being used, among other things. Finally, notice that the traceback is organized with the most recent call last. That means the line of code that is directly responsible for the error is at the bottom of the traceback message. 1.2 Error Statements The very last line in a traceback message is the error statement. An error statement has the following format: <Error type>: <Error message> This line provides you with two pieces of information: Error Type: The type of error that was caused. These are usually descriptive enough to help you narrow down your search for the cause of error. Error Message: A more detailed description of exactly what caused the error. Different error types produce different error messages. 1.3 Doctests One other (important) thing: don t forget to run doctests! For almost every assignment (homework, projects), we will provide you with doctests for certain functions they are there for your benefit. A quick run through the doctests will: CS61A Summer 2012 Page 2

Notify you of SyntaxErrors Check for basic functionality of your assignment, though passing doctests does not guarantee your assignment is entirely correct. To run a doctest, go to your terminal, cd to the directory in which your file is saved, and type: python3 -m doctest <assignment> This will run all the doctests contained in the file. Please use the doctests: it catches a lot of small, trivial errors (like improper indentation) that can nonetheless make it hard for your readers grade your assignments! 2 Common Errors The following are common errors that Python programmers run into. 2.1 SyntaxError Cause: Code syntax mistake File <File name>, line <number> def incorrect(f) ˆ SyntaxError: invalid syntax Solution: The ˆ symbol points to the location in the code that contains invalid syntax. The error message does not actually tell you what is wrong, but it does tell you where. Notes: Python will check for SyntaxErrors before executing any code. This is different from other errors, which are only raised during runtime. 2.2 IndentationError Cause: Improper indentation File <file name>, line <number> print( improper indentation ) ˆ IndentationError: unindent does not match any outer indentation level CS61A Summer 2012 Page 3

Solution: The line that is improperly indented is displayed. Simply re-indent it. Notes: Python will check for IndentationErrors before executing any code. 2.3 TypeError There are multiple possible causes: Invalid operand types for primitive operators. You are probably trying to add/subtract/multiply/divide incompatible types. Example: TypeError: unsupported operand type(s) for +: function and int Solution: Change the operand that is incorrect. The order of the operand types shown will narrow down which operand is of an incorrect type. In the above example, the first operand to the + operator is incorrect. Using non-function objects in function calls. Example: >>> square = 3 >>> square(3) Traceback (most recent call last):... TypeError: int object is not callable Solution: This bug usually happens when you accidentally assign a variable to a non-function object instead of a function. Double-check the code where you assign that particular variable. Passing incorrect number of arguments to a function. Example: >>> add(3) Traceback (most recent call last):... TypeError: add expected 2 arguments, got 1 Solution: Make sure you pass in the correct number of arguments to the specified function; the error message tells you how many arguments there should be, and how many you tried to pass. CS61A Summer 2012 Page 4

2.4 NameError Cause: Variable has not been assigned to anything, or it does not exist. This includes function names. File < file name >, line <number>, in <function> y = x + 3 NameError: global name x is not defined Solution: Make sure you are initializing the variable (i.e. assigning the variable a value / function object) before you use it. Notes: The reason the error message says global name is because Python will start searching for the specified variable from a function s local scope. If the variable is not found in the local scope, Python will keep searching outward until it reaches the global scope. If it still can t find a variable by that name in the global scope, Python will raise the error. 2.5 IndexError Cause: Trying to index a sequence (e.g. tuple, list, string) with a number that exceeds the size of the sequence. File < file name >, line <number>, in <function> s[100] IndexError: tuple index out of range Solution: Make sure the number you index with is within the bounds of the sequence. If you are using a variable as an index (e.g. seq[x]), make sure that the variable is assigned to a proper index. 3 Common Bugs The following are not errors that Python will complain about, but rather mistakes that programmers unintentionally make that Python will not be able to catch. 3.1 Spelling and Capitalization Remember that Python is case sensitive. The variable hello is not the same as Hello or hello or helo. CS61A Summer 2012 Page 5

This will usually show up as a NameError, but sometimes, the misspelled variable may actually have been defined. In that case, it can be difficult to find the error, and it is never gratifying to discover that it s just a spelling mistake. 3.2 Missing Parentheses One of the most common bugs is to leave off a closing parenthesis (or a closing single quote, or a closing double quote, or a closing bracket, or so on). If you see something like this: SyntaxError: EOL while scanning string literal or anything with an EOL message, you most likely forgot a closing marker. Solution? Add it back in. 3.3 = vs. == The single equal sign = is used for assignment; the double equal sign == is used for testing equivalence. The most common error of this form is something like if x = 3: 3.4 Order of Operations Remember that arithmetic is evaluated just like it would be in math. The following are listed from highest precedence to lowest: Parentheses: () Exponents: ** Multiplication: *, Division: / or //, and Modulo: % Addition: +, and Subtraction: - Boolean operators also have an order of precedence. As before, the list is organized from highest precedence to lowest: not: logical negation and: logical AND or: logical OR This means that the following expression will return True. False or not True and True or not False which is equivalent to saying CS61A Summer 2012 Page 6

False or ((not True) and True) or (not False) 3.5 Multiple Comparisons Many beginners try to write x and y are both greater than 0 as x and y > 0 However, Python (and most other languages) require you to explicitly apply the comparison to each variable: x > 0 and y > 0 3.6 Infinite Loops and Runaway Recursion Infinite loops are often caused by while loops; you might have forgotten to increment or decrement the iterating variable. For example, the following will continually print 0, never stopping. i = 0 while i > 10: print(i) Poorly written recursion will cause a large traceback to occur, followed by RuntimeError: Maximum recursion depth exceeded If that occurs, you mostly likely forgot to write a base case, or your base case may need to be fixed. 3.7 Acknowledgments This debugging guide was first written for CS61A, Summer 2012 by Albert Wu. CS61A Summer 2012 Page 7