4

I'm creating a document for students using quarto. One of the things I want to teach is how to read and understand error messages, so I was planning in creating wrong code blocks to force certain error messages (like the one below):

a_list = [1,2,"a"]
a_list[3]
# I want to generate an error to explain that python starts counting at 0

Regretfully, when I write the code above, quarto complains (with good reason) and will stop executing the rest of the code blocks and rendering the file.

Is there a way to get the error message while not stoping the execution?

I thought something like this would work, but it doesnt:

# | error: true
a_list[3]
ccamara
  • 1,141
  • 1
  • 12
  • 32

1 Answers1

5

The following works for me:

---
title: test-error
---    

```{python}
#| error: true
a_list = [1,2,"a"]
a_list[3]
```

It produces the following output:

The output generated by the snippet above, including "Index Error: list index out of range"

One thing to make sure is that the #| error: true line is at the beginning of the cell, and that there's no spaces between # and |.

Carlos Scheidegger
  • 1,906
  • 1
  • 13
  • 18
  • Thank you! I had a space between # and | ! I couldn't see it until I pasted your code from here and I had to use git, to see the change! Thanks for being my rubber duck, fellow Carlos. – ccamara May 25 '22 at 06:49
  • 1
    This is certainly confusing behavior on our part. In the case of Python, some code formatters don't like `#|`. We're actually going to change this to support spaces in the future, but we don't right now. – Carlos Scheidegger May 25 '22 at 15:35