Where a Python Script contains an error, it will typically be flagged to the user in the console at runtime. Python error messages are typically very helpful and make debugging Python code relatively straightforward. The console will typically display the line number of the problem code, as well as a ^ symbol showing the user where it thinks the problem has occurred. It is not always correct, however it will usually point the user in the right direction. In many cases the most effective approach to solving an error in Python code is to copy and paste the error message into a search engine such as Google. This section will detail some of the common Python errors that beginners may encounter.
The most common error a beginner will encounter is a syntax error. Syntax errors can have many different causes but are usually the result of a typo. They are typically very easy for the Python interpreter to find and easy for the user to correct.
Code |
Error Message |
Solution |
We need a : (colon) at the end of any if, else, elif, for, while, class, or def statement
Code |
Error Message |
Solution |
Used the assignment operator = instead of a comparison operator ==
This type of syntax error is due to the use of extended characters beyond the basic ASCII character set. Currently the VE Python API is not compatable with the use of characters such as: * Accented characters * Symbols * Non-Latin scripts
Indentation should only take place after a statement ending with a colon : The Python interpreter will return an indentation error if there is no indentation after a colon : or the code is indented where it should not be.
Code |
Error Message |
Solution |
Code on line 4 should not have been indented as there is no colon : at the end of line 3
A type error can occur if the type of an object is not what the Python interpreter expected to see for example if you try to concatenate (link) a non-string value to a string value.
Code |
Error Message |
Solution |
The int variable number must be explicitly converted to a string before it can be concatenated to the string room_name.
room_namePython raises a key error whenever a dictionary object is requested using a key that does not exist in that dictionary.
Code |
Error Message |
Solution |
The key room_name does not exist in the dictionary group. The key name does exist. The VE Scripts User Guide details the keys for every dictionary object in the VE Python API.
An attribute error will occur if a method that does not exist is called on an object. It can often be as a result of a simple typo in the method name. The VE Scripts User Guide details all of the methods for each object in the VE Python API.
Code |
Error Message |
Solution |
A typo in the method name. It should be get_room_groups() not get_room_groupz()
An argument error occurs if an incorrect number of arguments are passed into a method, the arguments are of the wrong type or the arguments are in the wring order. In this example Python interpreter returns a more complex error message, but it still points the user to line 12 where the error has occurred.
Code |
Error Message |
Solution |
get_room_groups() method takes one argument of the type int. The VE Scripts User Guide details all of the arguments for all methods for all objects in the VE Python API.
Many free online resources to help with troubleshooting:
https://inventwithpython.com/blog/2012/07/09/16-common-python-runtime-errors-beginners-find/
https://www.toptal.com/python/top-10-mistakes-that-python-programmers-make
https://pythonforbiologists.com/29-common-beginner-errors-on-one-page.html
http://www.dummies.com/programming/python/the-8-most-common-python-programming-errors/