Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

Similar presentations


Presentation on theme: "COSC 1306 COMPUTER SCIENCE AND PROGRAMMING"— Presentation transcript:

1 COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Jehan-François Pâris Fall 2017 1

2 THE ONLINE BOOK CHAPTER II DEBUGGING

3 Why this chapter? "Programmers spend 80 percent of their time chasing bugs and the remaining 20 percent creating new ones." Certainly true of bad programmers Many beginners become quickly discouraged when their programs do not do what they were supposed to do.

4 What is a bug? Anything that prevents our programs from doing what they are supposed to do Syntax errors Missing ending quotes, missing closing parentheses, missing columns Poor indentation Semantic error Inconsistent variable naming

5 Writing bug-free code Not really possible for most of us
Can reduce their number and severity A good approach Start small Write a partial solution to your problem Keep it working Add incremental steps Be sure each new step works before adding another one

6 Beginning tips Suspect everything but Python
Too many users have tested it Could be a wide gap between What your program actually does What you think it does Find clues

7 Narrowing the gap (I) Insert print statements
Anywhere you think there could be a problem print(xyz) or better print("xyz =", xyz) Works for all Python data types When you are done Comment these statements out # print("xyz =", xyz) Do not delete them until you are sure you will not need them

8 Narrowing the gap (II) Analyze your code Print it on paper if you can
Pretend to be the Python interpreter Execute each statement blindly Writing down the results at each step Works best after you have taken a break Repeat the process in front of a friend/TA You can let a friend look at your code but you cannot pass your program to any other student.

9 A good error message (I)
A very short program """ Converts miles into kilometers """ miles = float(input("Enter a mileage: ")) kms = miles*1609.4 print("That's %.2f kilometers" % km)

10 A good error message (II)
The program output Enter a mileage: 100 Traceback (most recent call last): File ".../badconvert.py", line 4, in <module> print("That's %.2f kilometers" % km) NameError: name 'km' is not defined

11 A good error message (III)
What it means """ Converts miles into kilometers """ miles = float(input("Enter a mileage: ")) kms = miles*1609.4 print("That's %.2f kilometers" % km) Misspelled the variable name kms

12 Another good error message (I)
Another error """ Converts miles into kilometers """ miles = float(input("Enter a mileage: ") kms = miles*1609.4 print("That's %.2f kilometers" % kms)

13 Another good error message (II)
Interpreter warns "EOL while parsing string literal" Problem was a missing quote """ Converts miles into kilometers """ miles = float(input("Enter a mileage: ")) kms = miles*1609.4 print("That's %.2f kilometers" % kms)

14 A confusing error message
Another mistake """ Converts miles into kilometers """ miles = float(input("Enter a mileage: ") kms = miles*1609.4 print("That's %.2f kilometers" % kms) Interpreter warns "invalid syntax"

15 The error A missing closing parenthesis
""" Converts miles into kilometers """ miles = float(input("Enter a mileage: ")) kms = miles*1609.4 print("That's %.2f kilometers" % kms)

16 The key ideas Start small
Use print statements whenever there is a doubt Look thoroughly at your code Be persistent

17 Using functions Let you decompose your program into a series of functions and function calls One of my programs 235 lines of code 12 functions All fairly short


Download ppt "COSC 1306 COMPUTER SCIENCE AND PROGRAMMING"

Similar presentations


Ads by Google