Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Create and Debug With Thonny

In order to get a practical idea of how you can use Thonny’s features, you’re going to create and debug a program. You’ll start by importing SimpleCalculator from calculator.simple and then create a new instance of SimpleCalculator.

Then, you’ll introduce a bug to the program. You’ll add a function with a docstring. After you try to call the function, you’ll use the debugger to see what went wrong. You’ll start by opening up the Variables window to see what’s happening as the program runs.

00:00 Creating and Debugging a Program. So, to bring together a number of those features, you’re going to see how to create and debug a program in Thonny. You’re going to start off by importing SimpleCalculator from calculator.simple. Next up, create a new SimpleCalculator instance with the my_calculator variable.

00:33 And then using code completion, hitting the Tab key, add the .run() method and feed it the information '2 * 2' as a string. Finally, print out my_calculator.lcd, save that as calculator_1,

00:58 and then run it. And we can see we come up with the correct answer, 4.0. So our program works.

01:10 Introducing a Bug.

01:16 Here, the program has been saved as calculator_2, and as you can see, you’re adding a function create_add_string(). You’re adding a docstring with triple quotes (''') to describe that this function should return a string containing an addition expression.

01:34 So in other words, we should be able to send two numbers to this and then return a string, which our simple calculator can then run. Finally, return 'x + y', so that’s returning that string for us. Now we’re going to alter line 8.

01:50 It’s going to use the create_add_string() function with 2 and 2. And now running the program gives us some result we weren’t expecting. The result’s 0, but we’re supposed to be adding 2 plus 2, so something must be wrong.

02:11 Let’s use the debugger to find out what’s going on.

02:19 Let’s open up the Variables window so we can see what’s happening as the program runs. And then debug the current script. We’re going to step into each function and each line.

02:35 Here we can see the SimpleCalculator() function. and then my_calculator has been created. And enough steps will take us into that create_add_string() function.

02:48 Here we can see we’re at the docstring. We’ll just expand that window so we can see the Local variables and you can see that x is 2 and y is 2. Step further on, we’ll see what gets returned, and we’re returning the text 'x + y'. That’s not what we were after, though.

03:11 What we wanted was to return '2 + 2', the values of x and y. So we’re returning 'x + y' to our calculator, there you can see that final return value there. When we do that, it’s now going to run 'x + y', literally on that string.

03:31 So then we have an output of None,

03:39 so it says print(0),

03:45 and that’s why our program is not working properly—because our function, instead of returning the value of x and the value of y, is returning literally the text 'x + y'. So let’s change that by altering that to an f-string which will substitute the values of x and y in, and now we can run the program and we can see that sanity has been restored.

04:11 We’ve used the debugging to find the problem and fix it, and now our calculator works as intended.

Avatar image for kwf777

kwf777 on Dec. 2, 2019

I know capitalization is important but I cannot get my code torun after changing it several times:

from calculator.simple import SimpleCalculator

def create_add_string(x,y):
    '''Retruns a string containing an addition expression'''
    return f'{x}+{y}'

my_calculator=SimpleCalculator()
mu_calculator.run(create_add_string(2,2))
print(my_calculator.lcd)

Shell:

 %Debug calculator.py
Traceback (most recent call last):
  File "/Users/kwf777/calculator.py", line 1, in <module>
    from calculator.simple import SimpleCalculator
  File "/Users/kwf777/calculator.py", line 1, in <module>
    from calculator.simple import SimpleCalculator
ModuleNotFoundError: No module named 'calculator.simple'; 'calculator' is not a package
>>>
Avatar image for Darren Jones

Darren Jones RP Team on Dec. 3, 2019

Hi kwf777. It looks like you haven’t installed the simplecalculator package. To do this, open Thonny, and go to Tools > Manage Packages, and then search for simplecalculator - the search should come up with a result of simplecalculator by Jacek Artymiak (currently at v0.0.4), and then press Install. If you then close that window and try running your program, it should work now.

Avatar image for kwf777

kwf777 on Dec. 4, 2019

It was showing as an installed package

Avatar image for Donald Walters

Donald Walters on Feb. 27, 2020

I’m certainly missing something conceptually I don’t understand how you can get calculator.simple from installing simplecalculator

from calculator.simple import SimpleCalculator

Avatar image for Ricky White

Ricky White RP Team on Feb. 28, 2020

Hi Donald. That is just the naming convention that the simplecalculator authors chose. So you’re not missing anything conceptually. You’ll find what you need to import in the documentation for any package on pypi (the Python Packaging Repository, where Thorny pulls it’s packages from).

Become a Member to join the conversation.