Basic Errors & Solutions For Python Beginners.

Date Posted: 13-10-2018

In this article, we are going to discuss a basic errors and  its solution in python. This article will help the beginners to know about  the errors and  guide them to solve it. let us detailed below,

1.Indentation Errors:

  • It generally occurs when there is inconsistency in the code means somewhere we have to use the  spaces and somewhere tabs for  Indentation.

 Indented block in python:

  • To indicate a block of code in Python, indent each line of the block by the same amount. The two blocks of code in If-statement are both indented four spaces, which is a typical amount of indentation for Python.
  • The lines of code that form an “if” block, or a “while” or “for” loop, or even an entire function/method or class, must all have the same level of indentation, otherwise the python will not execute .

2.Syntax Error:

  • An invalid syntax error means that there is a line that python doesn’t know what to do with. The last common type of syntax error you will likely encounter has to do with indention. Follow the  unintended does not match any outer indention level unexpected indent.

3.Often Occurring Indentation Error:

  • The unexpected indent and not intended are often occurring  errors.

4.Print Statement Should be at Exact Indent:

5.Flow path for syntax and indentation:

  • For every if, class, for ,function, this flow has to follow to free  from indentation.

 

6.Syntax Errors:

  • Syntax errors are the most basic type of error. They arise when the Python interpreter is unable to understand a line of code. Syntax errors are almost always fatal.
  • During execution, Most syntax errors are typos, incorrect indentation, or incorrect arguments. If you get this error, try looking at your scripted code for any of these.
def some_function()
    msg = "hello, world!"
    print(msg)
     return msg

Syntax Error , Where def function doesn’t have any (:) colon at its end.

 File "<ipython-input-3-6bb841ea1423>", line 1
    def some_function()
                       ^
SyntaxError: invalid syntax
  • In this case, the problem is that the function definition is missing a colon at the end.

7.Logic Errors:

  • These are the most difficult type of error to find, because they will give unpredictable results and may crash your program.  A lot of different things can happen if you have a logic error. However these are very easy to fix as you can use a debugger, which will run through the program and fix any problems.

8.Variable name Errors:

  • Name Error ,it occurs when you try to use a variable that does not exist. For your reference
print(a)

Error will shown as,

NameError   Traceback (most recent call last)
<ipython-input-7-9d7b17ad5387> in <module>()
----> 1 print(a)

NameError: name 'a' is not defined
  • Error messages are most Informative , which are usually of the form “name ‘the_variable_name’ is not defined”.The first and foremost thing  is that you will use a string but forgot to put quotes around it.
print(Bye)

 

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-9553ee03b645> in <module>()
----> 1 print(hello)

NameError: name 'hello' is not defined

9.Index Errors:

  • If you try to access an item in a list or a string that does not exist, then you will get an error.
  • Error Name: Index out of Range
letters = ['a', 'b', 'c']
print("Letter #1 is", letters[0])
print("Letter #2 is", letters[1])
print("Letter #3 is", letters[2])
print("Letter #4 is", letters[3])

Letter #1 is a
Letter #2 is b
Letter #3 is c

---------------------------------------------------------------------------
IndexError  Traceback (most recent call last)
<ipython-input-11-d817f55b7d6c> in <module>()
      3 print("Letter #2 is", letters[1])
      4 print("Letter #3 is", letters[2])
----> 5 print("Letter #4 is", letters[3])

IndexError: list index out of range


10.File Errors:

  • File Errors are those occur due to incorrect path / not mentioning a detailed path will raise a error.
  • Error Name 1 : FileNotFoundError
  • Error  Name 2 : Unsupportedoperation
file_handle = open('myfiles.txt', 'r')
Traceback (most recent call last)---
------------------------------------------------------------------------
FileNotFoundError  
<ipython-input-14-f6e1ac4aee96> in <module>()
----> 1 file_handle = open('myfiles.txt', 'r')

FileNotFoundError: [Errno 2] No such file or directory: 'myfiles.txt'

Where the exact location of myfiles.txt, it should be mentioned in a following manner. If the myfiles.txt is located in Myproject/local/myfiles.txt, Declare in the script with detailed manner, Otherwise it will raise a FileNotFoundError.

A related issue can occur if you use the “read” flag  (r) instead of the “write” flag(w). Python will not give you an error if you try to open a file for writing when the file does not exist. However, if you meant to open a file for reading, but accidentally opened it for writing, and then try to read from it, you will get an UnsupporetedOperation error telling you that the file was not opened for reading:

file_handle = open('myfiles.txt', 'w')
file_handle.read()
---------------------------------------------------------------------------
UnsupportedOperation  Traceback (most recent call last)
<ipython-input-15-b846479bc61f> in <module>()
      1 file_handle = open('myfiles.txt', 'w')
----> 2 file_handle.read()

UnsupportedOperation: not readable

These are the most Basic and common errors will occur for the python Beginners. I hope this article will give you a information ,how to fix those errors and what are the root cause of the Errors. Follow this article and improve your skills in python.Thank you and Leave a comments below.

 

 

 

 

1 thought on “Basic Errors & Solutions For Python Beginners.”

Leave a Reply