Common errors and messages, Python Programming

Common Errors and Messages

Here are some common Python errors  and error  messages to look  out for. Please let us give if you have any favorite additions for this list.

  • A problem about NoneType often means you forgot a return.

def plus1 (x):

 

x + 1

>>> y = plus1(x)

>>> plus1(x) + 2

Traceback (most recent call last):

File "", line 1, in

TypeError that shows: unsupported operator type(s) for +: 'NoneType' and 'int'

 

  • Weird results from math can give from integer division

>>> 3/ 9

0

  • "Unsubscriptable object" seems you are trying to get an element out of something that isn't a dictionary, list, or tuple.

 

>>> x = 1

>>> x[3]

Traceback (most recent call last):

File "", line 1, in

TypeError that shows: 'int' object is unsubscriptable

 

  • "Object is not callable" defines that you are trying to use something that isn't a procedure or method as if it were.

 

>>> x = 1

>>> x(3)

Traceback (most recent call last):

File "", line 1, in

TypeError that shows: 'int' object is not callable

 

  • "List index out of range" describes that you are trying to read or write an element of a list that is not present.

 

>>> a = range(5)

>>> a

[0, 1, 2, 3, 4]

>>> a[5]

Traceback (most recent call last):

File "", line 1, in

IndexError that shows: list index out of range

 

  • "Maximum recursion depth exceeded" produces the error has a recursive procedure that is nested your parent case is not being reached due to a bug.

 

def fizz(x):

return fizz(x - 1)

>>> fizz(10)

Traceback (most recent call last): File "", line 1, in File "", line 2, in fizz

File "", line 2, in fizz

...

File "", line 2, in fizz

RuntimeError that shows message: maximum recursion depth exceeded

 

  • "Key Error " defines that you are trying to look up an element in a dictionary that is not present.

 

>>> d = {'a':7, 'b':8}

>>> d['c']

Traceback (most recent call last):

File "", line 1, in

KeyError: 'c'

 

 

  • Another common error is forgetting the self before calling a function. This creates the same error that you would get if you tried to call a function that wasn't de?ned at all.

Traceback (most recent call last): File "", line 1, in File "V2.py", line 22, in  

 

add    

return add(v)

NameError: global name 'add' is not defined

 

 

Posted Date: 8/9/2012 7:22:12 AM | Location : United States







Related Discussions:- Common errors and messages, Assignment Help, Ask Question on Common errors and messages, Get Answer, Expert's Help, Common errors and messages Discussions

Write discussion on Common errors and messages
Your posts are moderated
Related Questions
When investing money, an important concept to know is compound interest. The equation FV = PV (1+rate)periods . This relates the following four quantities. The present value (

Every programming language has to have  some method for indicating grouping of operations. Here is how you execute an if-then-else structure in Java:   if (s == 1){ s = s

what is python

Write a Python program to accomplish the following. Use modular design. Include at least 3 functions: one that returns zero values, one that returns one value, and one that retu

There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many

Basics of python-introduction Python is designed for easy interaction between a user and the system. It goes with  an inter­ active function  known as  a shell or listener.  T

List Comprehensions   Python has a very nice built-in  facility for doing  many  iterative methods, known as list comprehensions. The basic template is       [

For and While   If we want  to do some operation or set of operations various  times, we can handle the process  in several  different types.  The most straightforward types

1 Low Level Exploits 1.1 Savegames Jimmy is becoming increasingly frustrated at the computer game hes playing. He has a save right before the levels boss but he needs either more

Write an algorithm for a program that shows the use of all 6 math functions. Write, test, and debug the program using Python. SAMPLE OUTPUT (not including author/program infor