Skip to main content
Logo image

Discrete Math with SageMath: Learn math with open-source software

Section 1.7 Debugging

Error messages are an inevitable part of programming. When you encounter one, read it carefully for clues about the cause. Some messages are clear and descriptive, while others may seem cryptic. With practice, you will develop valuable skills debugging your code and resolving errors
Note that not all errors result in error messages. Logical errors occur when the syntax is correct, but the program does not produce the expected result. Usually, these are a bit harder to trace.
Remember, mistakes are learning opportunities —everyone makes them! Here are some useful debugging tips:
  • Read the error message carefully —it often provides useful hints.
  • Consult the documentation to understand the correct syntax and usage.
  • Google-search the error message —it’s likely that others have encountered the same issue.
  • Check SageMath forums for previous discussions.
  • Take a break and return with a fresh perspective.
  • Ask the Sage community if you’re still stuck after trying all the above steps.
Let’s dive in and make some mistakes together!
A SyntaxError usually occurs when the code is not written according to the language rules.
Why didn’t this print Hello, World! to the console? The error message indicates a SyntaxError: invalid decimal literal. The issue here is the invalid variable name. Valid identifiers must:
  • Start with a letter or an underscore (never with a number).
  • Avoid any special characters other than the underscores.
Let’s correct the variable name:
A NameError occurs when a variable or function is referenced before being defined.
Sage assumes Hi is a variable, but we have not defined it yet. There are two ways to fix this:
  • Use quotes to indicate that Hi is a string.
  • Alternatively, if we intended Hi to be a variable, then we must define it before first use.
Reading the documentation is essential to understanding the proper use of methods. If we incorrectly use a method, we will likely get a NameError (as seen above), an AttributeError, a TypeError, or ValueError, depending on the mistake.
Here is another example of a NameError:
The sort() method must be called on the list object using dot notation.
An AttributeError occurs when an invalid method is called on an object.
The len() function must be used separately rather than as a method of a list.
A TypeError occurs when an operation or function is applied to an incorrect data type.
The append() method only takes one argument. To add multiple elements, use extend().
A ValueError occurs when an operation receives an argument of the correct type but with an invalid value.
Although the resulting error message is lengthy, the last line informs us that Factorials are only defined for non-negative integers.
A Logical error does not produce an error message but leads to incorrect results.
Here, assuming your task is to print the numbers from \(1\) to \(10\text{,}\) and you mistakenly write the following code:
This instead will print the numbers \(0\) to \(9\) (because the start is inclusive but not the stop). If we want numbers \(1\) to \(10\text{,}\) we need to adjust the range.
To learn more, check out the CoCalc article
 1 
github.com/sagemathinc/cocalc/wiki/MathematicalSyntaxErrors
about the top mathematical syntax errors in Sage.