OO Programming and Data Structures | CS 241

05 Prepare : Checkpoint B

Objective

Practice using an IDE / debugger.

You are encouraged with work with others for this checkpoint.

Overview

After completing (or while completing) the preparation material for this week, complete the following exercise.

For this exercise, you will practice using Thonny to debug a program. Unlike other checkpoints, you will not submit any code, but rather, will indicate your progress via the quiz in I-Learn.

Please recognize that the purpose of this assignment is to help you learn about a tool that will make your life easier in the coming assignments. You should view it as a tutorial to get you started, not as a series of steps to check off to get credit.

Instructions

Running the file

  1. Follow the installation instructions from the preparation material to install Python 3, the Arcade library, and Thonny on your own system.

  2. Open Thonny and create a new Python file. Copy and paste the contents of the check05b.py starting template. Then save it as a file named, check05b.py

    This file contains a simple Money class that holds a dollars and cents, and then a main function that creates some different accounts and alters the balance of them.

  3. Click the green "play" button on the toolbar to run the current script.

  4. Ensure that you can see the output of the file in the output window at the bottom of the IDE.

Debugging and Stepping over code

  1. Start the debugger by clicking the click the Debug tool bar button (the one that looks like a little "bug"). This starts running the program in a mode where it can run line by line, and you will see the line that is currently running as well as the value of each variable in that function.

  2. Once you have started debugging the program, scroll down to the main function. Find the line where account2 is displayed for the first time. Put your cursor on this line, and then click "Run to Cursor" from the Run menu. This will run everything in the program up until that line, and then stop there.

    In the local variables of the main function popup, you should see one account listed (account1) because the others haven't been created yet.

  3. Allow the program to run one more step by clicking the "Step Over" button. Ensure that the second account is displayed.

  4. Continue clicking the step over button, and watch your program run one step at a time. Notice that when you step over the display function calls, the values are printed to the console of the main window. When you are ready for it to finish main, click the "step out" button which finishes the function, then you can click it once more to step out of that scope and finish the program.

Stepping into and out of functions

  1. In the main function, find the line that adds 20 cents to account 1. Start the debugging, put your cursor on that line and run to it.

  2. This time, instead of stepping over that line of code, we will step into that function to see what it is doing. Find and click the arrow that corresponds to "Step Into" and click it. The program should now take you to the add_cents method of the Money class.

  3. Click the step over button to step over these lines of code until you are back to the main function again and on to the line where 20 cents are added to account2. Click "Step Into" again to go into the method.

  4. This time, rather than stepping through each line of code to get back to main, find and click the "Step Out" button. This finishes the method and resumes code back in main.

  5. Continue playing around with stepping into and out of functions until you feel comfortable with it. Make sure that you can step all the way into the "check_overflow" method.

Inspecting variables

  1. Once again, start the debugger, put your cursor on the the line of code in main that will add 20 cents to account1 and let it run to this point.

  2. This time, instead of looking at the "Console" window, notice the variables in the Local variables part of the window for the main function.

  3. In the variables window, find the account1 variable, notice the current dollars and cents value. (You might be interested to know that the string that renders in this variable window is defined in the __repr__ method of the class.)

  4. Now step over that line of code and notice the change to the dollar and cents.

  5. Next, step into the add_cents method for account2. Notice that a new window has popped up showing the the variables of this function. Notice that you can now see the representation of the self variable.

  6. Step over the lines of code to see their effect on the member variables.

  7. Repeat this process for watching it add 20 cents to account3.

  8. Continue playing around with watching variables until you feel comfortable with it.

Tracking down a bug

As you may have noticed this code has a bug in it. The account3 balance has 99.99 cents at the start. Then, 20 cents are added and the final balance is 99.19, not 100.19 as it should be.

Use your new debugging skills to step into the add_cents method for account3 and keep an eye on the variables to see if you can find what the bug is. Then, fix the bug, walk through the code and make sure everything looks correct.

Submission

For this assignment, you will not submit any code to the Linux system like you normally do. Instead, please answer the questions in the accompanying I-Learn quiz.