Python Programming – 5 must know errors and resolutions
Learning Python? Find out about most common errors and their resolutions
A Python program terminates as soon as it encounters an error. In Python, an error can be a syntax error or an exception. In this epmwise article, we walk you through some of the main errors and with their resolutions.
NameError means that the you tried to use the variable that is not defined I the code before. This error occurs when you mistype the variable name, didn’t define the variable or forgot to include quotes in the string. Find more about this error here
You can also get the NameError if you use raw_input instead of input in python 3.
2. Type Error: It is a runtime Python error that is raised due to type mismatch. This can happen if you pass arguments of the wrong type.
This post on Stack-overflow has nice write-up about Type Error.
3. Indentation Error: Indentation plays a vital role in readability of the code. Python enforces indentation while many programming languages do not. You can get two indentation errors: expected an indented block or unexpected indent.
Expected an indented block error is encountered when you forget to add indentation to loops, conditional statement or functions where indentation is necessary. Unexpected indent occurs when you add indentation where it is not needed.
4. KeyboardInterrupt: This type of python error occurs when execution of the program is interrupted. It commonly occurs when you press Ctrl+C in the console.So, make sure if you are trying to copy something, you do that in command window and not in console.
5. KeyError: This python error is encountered when the mapping (dictionary) key is not present in the dictionary.
This error can be avoided using get() method. If you are not sure whether the key is present in the dictionary, you should always use the get method instead of directly accessing the key. For more information on Key Error, Click here.
Hope you liked the information presented above. Please share your favorite errors, resolutions and ideas on your favorite programming language.