29_common_beginner_python_errors.pdf

Upload: kushan-shah

Post on 14-Apr-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 29_common_beginner_Python_errors.pdf

    1/1

    My code isn't working :-(

    Do you get anerror when yourun the code?

    What type

    of error do

    you get?

    Does the codeuse loops or ifstatements?

    A list which should have

    a value for every iterationonly has a single value

    You have defined the listinside the loop: move it

    outside

    A loop which uses therange function misses

    out the last valueThe range function isexclusive at the finish:

    increase it by one.

    I am trying to loop over a

    collection of strings, butam getting individual

    charactersYou are iterating over a

    string by mistake

    I am trying to writemultiple lines to a file butonly getting a single one

    You have opened the fileinside the loop: move it

    outside

    A regular expression isnot matching when I

    expect it toYou have forgotten to use

    raw strings or escapebackslash characters

    I'm trying to print a valuebut getting a weird-

    looking string

    You are printing an object(e.g. a FileObject) whenyou want the result of

    calling a method on theobject

    A number which shouldbe a fraction is comingout as zero in Python 2

    You are dividing integersrather than floats.

    Convert the numbers tofloats or from __future__

    import division

    I am reading a file but getting no inputYou have already read the contents of the file earlier

    in the code, so the cursor is at the end.

    A variable that shouldcontain a value does not

    You are storing the return

    value of a function whichchanges the variable

    itself (e.g. sort)

    Do you get anerror when yourun the code?

    What type

    of error do

    you get?

    Does the codeuse loops or ifstatements?

    Two numbers which shouldbe equal are not

    You are comparing a numberwith a string representationof a number (e.g. if 3 == "3")

    A complex condition is notgiving the expected result

    The order of precedence in the

    condition is ambiguous - addsome parentheses

    Do you get anerror when yourun the code?

    What type

    of error do

    you get?

    Does the codeuse loops or ifstatements?

    Do you get anerror when yourun the code?

    What type

    of error do

    you get?

    Start here...

    loops

    neither

    if

    no

    yes

    also check...

    IndentationError

    SyntaxError

    TypeError

    NameError

    AttributeError

    IOError

    KeyError

    You've forgotten thequotes around a string

    You have forgotten toput a colon at the end

    of a def/if/for line

    You have differentnumber of open andclose brackets in a

    statement

    You've called a method/function with the wrong

    number or type ofarguments

    You're trying to use anoperator on the wrong

    type of objects

    An object which youexpect to have a value is

    actually None

    You've used non-integernumbers in a list slice

    You've used a mixture

    of tabs and spacesYou haven't indented alllines in a block equally

    You've misspelt a variable,function or method name

    Your code calls a functionbefore it's defined

    Your code uses avariable outside the

    scope where it's defined

    You've forgotten toimport a module

    You've forgotten todefine a variable

    You're trying to print asingle word and haveforgotten the quotes

    You are calling amethod on the wrong

    type of object

    You're trying to open afile that doesn't exist

    You're trying to look

    up a key that doesn'texist in a dict

    http://pythonforbiologists.com