184_notes:troubleshooting

Coding: When it all Goes Wrong

A programming project isn't complete without an error that seems impossible to fix. Glowscript's error messages can be vague. If you find yourself completely lost in a coding project, the best bet is usually to take a step back. Go back to your pseudocode and comments, and compare them to the code you have written. Is each line doing exactly what it should be? Be particularly careful with loops and if statements, and consider the location of each line within a loop.

Often, something you've written isn't doing what you expect it to. The best way to track these issues down in Glowscript is using print() statements. Pick a value, and print it out. Then check if the printed value matches what you expect. Try a few if you're having trouble tracking down an error.

Another good strategy is to comment out code that you suspect is causing an error, then add it back in line-by-line until you find the source of the error.

Sometimes, the issue is with the way your code is written (syntax). These cases can be particularly hard to track down.

Some common issues show up in math syntax

Implicit Multiplication

The following does not work in Python:

a = 2
5_times_a = 5a

Instead, you must use an asterisk to signify multiplication:

a = 2
5_times_a = 5*a

Exponents

The caret (^) symbol is not used for exponents, instead use a double asterisk (**). This won't cause an error, but it won't do what you want it to.

x**2  # Correct
x^2   # Not what you want

Divide by Zero

If your code contains division by 0, you may get a confusing error message. Double-check that variables are properly initialized, correctly positioned within loops, and that any counter variables are being incremented in the correct locations.

If the conditional initially evaluates to False, the loop will never execute.

Are you resetting a variable that shouldn't be inside the loop?

Are you not resetting a variable that should be inside the loop?

The Glowscript version number is specific. You should only have one of these lines at the top of your code:

GlowScript 2.7 VPython

It should match the one provided with your source code for the project.

  • 184_notes/troubleshooting.txt
  • Last modified: 2022/05/06 22:06
  • by woodsna1