Debugging We ’ ve seen how to write test cases to help you find bugs in your program. What should you do when you find something is wrong? ? Find the line that ’ s wrong and fix it – Which line is wrong? – We know the symptons , not the disease – So our job is like that of a doctor – Find out what ’ s making the patient sick! Debugging ? Sometimes just reading and tracing the program is enough – And once upon a time that was the only option – But doctors can run tests, ask questions …– If we could interact with the program while it ’ s running … we might be able to see the bug as it happens The debugger ? The debugger is a tool for controlling and inspecting a program as it runs – Part of most IDEs (WingIDE and IDLE for sure) – It does not find the bugs for you – Instead, it ?“ Slows down ” the program by letting you run it one line at a time ? Shows you variables and their values ? Shows what functions are currently running ( “ call stack ”) Breakpoints When using the debugger, you usually start by setting a breakpoint. ? This tells the debugger to pause the program just before it executes that line. ? In WingIDE, click next to the line number – A red dot appears to show there is a breakpoint there. – Click it again to turn it off – Note: breakpoints are not saved with the program! ? In IDLE, right click the line of code and choose “ Set breakpoint ”. – The line will be highlighted in yellow Breakpoints ? To run with the debugger in WingIDE, click the “ Debug ” icon, the program will start running ? In IDLE select Debug → Debugger then run the program normally (using Run Module) ? The program will run full-speed until control reaches the breakpoint ? Then the program pauses, the IDE gives you control ? If you run the program without using “ Debug ”, breakpoints are ignored, the program runs normally. ? You may have to put in more than one breakpoint if you have branches or loops, to make sure execution actually reaches one of them S