|
| 1 | +--- |
| 2 | +title: "The Python Sandbox" |
| 3 | +description: "Tutorial: running and debugging Python code directly in your browser" |
| 4 | +order: 6 |
| 5 | +--- |
| 6 | + |
| 7 | +# The Python Sandbox |
| 8 | +For Python exercises, Dodona has a built-in **sandbox**: a window in which you can write and run Python code directly in your browser, without installing anything on your computer. The sandbox is ideal for quickly trying something out: experiment with a few lines of code, test your solution with your own input, or step through your program with the built-in debugger to see exactly what it does. Code you run in the sandbox is **not** submitted to Dodona and is not evaluated by the judge; when you are happy with your code, you submit it in the usual way, as explained in [Solving Exercises](../exercises/). |
| 9 | + |
| 10 | +## Opening the Sandbox |
| 11 | + |
| 12 | +The sandbox is available on the page of every Python exercise. At the bottom of the panel where you submit your solution, next to the submit button, you will find the `To sandbox` button. Clicking it opens the `Python sandbox` in a panel on top of the exercise page. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +The sandbox consists of two halves: |
| 17 | + |
| 18 | +- On the left you will find a **code editor** with a `Run` button, a field to provide **input**, and the **output** of your code. |
| 19 | +- On the right, the `Description` tab shows the exercise description, so you can keep reading the assignment while you write your code. |
| 20 | + |
| 21 | +If you had already written code in the editor of the exercise page, the sandbox automatically starts from that code. If the sandbox already contains different code from an earlier session, Dodona first asks whether you want to overwrite it with your latest changes. |
| 22 | + |
| 23 | +::: tip The first run can take a moment |
| 24 | +The sandbox runs Python entirely in your browser. The first time you run code, your browser still has to download and start the Python environment, so it can take a little while before you see any output. Subsequent runs are much faster. |
| 25 | +::: |
| 26 | + |
| 27 | +## Running Code |
| 28 | + |
| 29 | +Write your code in the editor on the left and click `Run`. The output of your program appears in the output panel below. While your code is running, you can interrupt it at any time with the `Stop` button. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +Some more things the sandbox can do: |
| 34 | + |
| 35 | +- **Input**: if your program reads input (for example with `input()`), you can simply type your answer in the input field when your program asks for it, and press enter. Prefer to prepare all input beforehand? Click `Switch to batch input` and enter all input lines in advance; `Switch to interactive mode` takes you back. |
| 36 | +- **Turtle graphics**: if your code draws with the `turtle` module, the drawing appears in a separate `Turtle` tab next to the textual output. |
| 37 | +- **Packages**: when your code imports a package, the sandbox automatically tries to install it. Most commonly used packages work; packages that need access to your operating system do not work in the browser. |
| 38 | +- **Files from the exercise**: files that come with the exercise can be loaded into the sandbox automatically, and you can drag links or images from the exercise description into the sandbox to use them in your code. |
| 39 | + |
| 40 | +## Submitting Your Code |
| 41 | + |
| 42 | +Code in the sandbox is never submitted automatically. Once your code does what you want, click `Finalize and submit`: this copies your code from the sandbox to the editor of the exercise page, where you can review it one last time and submit it. Only then is your solution evaluated by the judge, as described in [Understanding Feedback](../feedback/). |
| 43 | + |
| 44 | +::: warning Sandbox code is not saved |
| 45 | +Dodona does not store the code you write in the sandbox. If you close the page without copying your code to the editor and submitting it, your work may be lost. Submit regularly! |
| 46 | +::: |
| 47 | + |
| 48 | +## Debugging Step by Step |
| 49 | + |
| 50 | +The sandbox also contains a **debugger** (based on the well-known Python Tutor) that executes your program step by step. This is very useful if your program does something different from what you expected: instead of guessing, you can watch exactly what happens at each step. |
| 51 | + |
| 52 | +Click the `Debug` button next to `Run` to start the debugger. The right half of the sandbox switches to the `Debugger` tab, where you can walk through your program: |
| 53 | + |
| 54 | +- Drag the **slider** to move through the steps of your program, or use the arrow buttons to go to the previous, next, first or last step. |
| 55 | +- At each step, the debugger shows which line is being executed and how your program builds and stores information: the variables that exist at that point and their values. |
| 56 | +- Click `Stop debugging` to return to normal mode. |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +### Debugging a Failed Test Case |
| 61 | + |
| 62 | +You can also start the debugger straight from the feedback on a submission. On the feedback page of a Python exercise, test cases show a `Debug` button. Clicking it opens the sandbox with the code of your submission, runs that specific test case with the same input the judge used, and opens the `Debugger` tab. Above the debugger, under `Test case being debugged`, you can see which test case you are stepping through. This way you can follow, step by step, exactly where your submission goes wrong for a failed test. |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +## Limitations |
| 67 | + |
| 68 | +The sandbox is a tool for experimenting, not a replacement for submitting: |
| 69 | + |
| 70 | +- Your code runs in your browser, not in the environment the judge uses. Running code in the sandbox never produces a submission, and only the judge determines whether your solution is correct. |
| 71 | +- Not all packages are available in the browser, and heavy computations may run slower than on the judge. |
| 72 | +- Sandbox code is not saved by Dodona: copy your code to the editor and submit it to keep it. |
| 73 | + |
| 74 | +The sandbox is only available for **Python** exercises. For other programming languages, we recommend working in an [IDE](../exercises/#submitting-a-solution) and submitting your solution on Dodona. |
0 commit comments