AttributeError ============== AttributeError: XXX object has no attribute YYY ----------------------------------------------- An attempt was made to apply a method (indicated by *YYY*) to the object *XXX* but the method does not exist for the type of *XXX*. A particular case occurs when a module is imported with the ``import`` statement and an attempt is made to call a function of this module that does not exist in the file. **Examples** In this example, an attempt is made to apply the ``replace`` method to a list but this method does not exist for this type. .. literalinclude:: exemples/objecthasnoattribute1.py :language: python :linenos: .. literalinclude:: exemples/objecthasnoattribute1.txt :language: python In this example ``filename`` is a string parameter that contains the name of a file. The notation ``filename.txt`` contains a string object followed by a dot and, therefore, is interpreted by Python as a call to the ``txt`` method which must be a string method, but this method does not exist and gives an error. .. literalinclude:: exemples/objecthasnoattribute2.py :language: python :linenos: .. literalinclude:: exemples/objecthasnoattribute2.txt :language: python This example corresponds to the particular case mentioned above. The ``inventory`` module is imported and then the module function called ``total`` is called. The error indicates that there is no function in the ``inventory.py .. literalinclude:: exemples/objecthasnoattribute3.txt :language: python FileNotFoundError ================= FileNotFoundError: [Errno 2] No such file or directory: XXX ----------------------------------------------------------- An attempt was made to open a file named *XXX* for reading that does not exist. **Example** In this example, line 2 attempts to open the file ``inventario.txt`` for reading, but Python cannot find this file. Often the problem is that it is not the correct name or the file is not in the directory (folder) where we are located. .. literalinclude:: exemples/filenotfound1.py :language: python :linenos: .. literalinclude:: exemples/filenotfound1.txt :language: python ImportError =========== ImportError: No module named XXX -------------------------------- An attempt was made to import a module named *XXX.py** but no file with this name exists. **Example** In this example, we try to import the ``conversions.py`` module but it gives an error because Python cannot find this file. Often the problem lies in the fact that it is not the correct name or the file is not in the directory (folder) where we are located. .. literalinclude:: exemples/nomodulenamed1.txt :language: python IndentationError ================ IndentationError: expected an indented block -------------------------------------------- There are missing spaces in front of a statement that goes inside a code block in an ``if``, ``while``, ``for``, etc. **Example** In this example, the statement that must be executed if the condition is met cannot go at the same height as the ``if``. .. literalinclude:: exemples/expectindent1.py :language: python :linenos: .. literalinclude:: exemples/expectindent1.txt :language: python IndentationError: unexpected indent ----------------------------------- Excess spaces before a statement. **Example** In this example, line 3 has more spaces than necessary and should be at the height of the previous line. .. literalinclude:: exemples/unexpectindent1.py :language: python :linenos: .. literalinclude:: exemples/unexpectindent1.txt :language: python IndentationError: unindent does not match any outer indentation level --------------------------------------------------------------------- The spacing of a statement does not match any spacing level of the others. **Example** In this example, the statement on line 4 is not aligned with either line 1 or line 2 or 3. In this case, it should have one more space. .. literalinclude:: exemples/unindentdoesnotmatch1.py :language: python :linenos: .. literalinclude:: exemples/unindentdoesnotmatch1.txt :language: python IndexError ========== IndexError: XXX index out of range ---------------------------------- An attempt was made to access the position of a value (of the type indicated by *XXX*) using an index that is not within the allowed range. **Example** In this example, the function receives the string ``text`` as a parameter and assigns the length of this string to the variable ``ultima`` (line 2). Later (line 3) an attempt is made to access the position indicated by ``ultima`` which is outside the allowed range and gives an error. The indices of a string start from 0 and so the maximum allowed index is the length minus 1. .. literalinclude:: exemples/indexoutrange1.py :language: python :linenos: .. literalinclude:: exemples/indexoutrange1.txt :language: python KeyError ======== KeyError: XXX ------------- An attempt was made to access a value in a dictionary through the key *XXX* but this key does not exist. **Example** In this example, the keys of the dictionary are the strings 'BUS', 'BICING' and 'METRO' and therefore the attempt to access through the key 12 gives an error. .. literalinclude:: exemples/keyerror1.txt :language: python NameError ========= NameError: name XXX is not defined ---------------------------------- A reference was made to a name (indicated by *XXX*) that Python does not know because it is not in the language or has not been defined. **Exemples** Calling a function with a different name than the one used in the definition. In this case, the call ``convseconds`` is incorrect because we have defined the function as ``conv_seconds``. .. literalinclude:: exemples/namenotdefined1.py :language: python :linenos: .. literalinclude:: exemples/namenotdefined1.txt :language: python Using functions from an external module that has not been imported. In this case, the call to the ``sin`` function is incorrect because this function is in the ``math`` module and has not been imported. Once imported and called correctly, it works. .. literalinclude:: exemples/namenotdefined2.txt :language: python SyntaxError =========== SyntaxError: invalid syntax --------------------------- The syntax does not conform to Python writing rules. **Exemples** Using operators incorrectly. In this case, the expression that calculates 2 multiplied by the variable k is incorrect because the multiplication operator ``*`` is missing. .. literalinclude:: exemples/invalidsyntax1.py :language: python :linenos: .. literalinclude:: exemples/invalidsyntax1.txt :language: python Use ``=`` instead of ``==`` to make comparisons. .. literalinclude:: exemples/invalidsyntax2.py :language: python :linenos: .. literalinclude:: exemples/invalidsyntax2.txt :language: python Not writting ``:`` at the end of the statements that require it (if, elif, else, for, while, def). In this example, they need to be put at the end of the ``if`` statement. .. literalinclude:: exemples/invalidsyntax3.py :language: python :linenos: .. literalinclude:: exemples/invalidsyntax3.txt :language: python Use a Python reserved word as a variable name (in this example in) .. literalinclude:: exemples/invalidsyntax4.py :language: python :linenos: .. literalinclude:: exemples/invalidsyntax4.txt :language: python SyntaxError: EOL while scanning string literal ---------------------------------------------- Missing quotes (single or double, depending on the code) to open or close a string. **Example** En aquest exemple falta tancar la cometa de l´últim string a la línia 2. .. literalinclude:: exemples/eolscanning1.py :language: python :linenos: .. literalinclude:: exemples/eolscanning1.txt :language: python SyntaxError: EOF while scanning triple-quoted string literal ------------------------------------------------------------ **Example** .. literalinclude:: exemples/eolscanningtriple1.py :language: python :linenos: .. literalinclude:: exemples/eolscanningtriple1.txt :language: python TypeError ========= TypeError: Can't convert XXX object to str implicitly ------------------------------------------------------- An attempt was made to use a string operation with some other data type (indicated by *XXX*) **Example** In this example, line 4 tries to concatenate an integer to the variable s that contains a string. .. literalinclude:: exemples/cantconvertstr1.py :language: python :linenos: .. literalinclude:: exemples/cantconvertstr1.txt :language: python TypeError: unorderable types: XXX > YYY ------------------------------------------------------- An attempt was made to compare two different types (*XXX* and *YYY* indicate the types) that cannot be compared because they are not ordered between them. **Example** In this example the variable ``prod_code`` contains a string and an attempt is made to compare with an integer. .. literalinclude:: exemples/unorderabletypes1.txt :language: python TypeError: can only concatenate XXX (not YYY) to XXX ---------------------------------------------------- An attempt was made to concatenate values ​​of two different types (indicated by *XXX* and *YYY*). **Example** In this example, the ``intersection`` function has been executed with the values ​​``list1=[1,2,3]`` and ``list2=[2,3,4,5]``. On line 5, an attempt is made to concatenate to the variable ``lres``, which is a list, a value from ``list1`` that is an integer. .. literalinclude:: exemples/canonlyconcat1.py :language: python :linenos: .. literalinclude:: exemples/canonlyconcat1.txt :language: python TypeError: XXX indices must be integers, not YYY ------------------------------------------------ An attempt was made to access an element of an indexed type (such as a list) **Example** This example attempts to access a position in a string using another string. When an integer is used (in this case 12) the access is executed correctly. .. literalinclude:: exemples/indicesmustbe1.txt :language: python TypeError: unsupported operand type(s) for XXX: YYY and ZZZ ----------------------------------------------------------- An attempt was made to apply an operator (indicated by *XXX*) to values ​​of a type that does not accept this type of operation (indicated by *YYY* and *ZZZ*). **Exemples** In these examples, an attempt is made to apply numeric type operands (division, addition, and subtraction) to types that do not accept this type of operation such as strings or lists. .. literalinclude:: exemples/unsupportedop1.txt :language: python TypeError: bad operand type for unary XXX: YYY ---------------------------------------------- An attempt was made to apply a unary operator (indicated by *XXX*) to a value that does not accept this type of operation (indicated by *YYY*). **Example** This example applies the sign change operation to a string, a type that does not accept this operation. .. literalinclude:: exemples/badoptypeunary1.txt :language: python TypeError: XXX object is not subscriptable -------------------------------------------- An attempt was made to access an element through an index to a value of a type (indicated by *XXX*) that does not accept indexing, or an attempt was made to obtain a *slice* of a type that does not accept the operation. **Example** In this example, the values ​​taken by the variable ``i`` are integers, since they are generated from the ``for-in-range`` statement. On line 4 of the function, an attempt is made to access position 0 of the variable ``i``, which gives an error because integers cannot be indexed. .. literalinclude:: exemples/objectnotsubscript1.py :language: python :linenos: .. literalinclude:: exemples/objectnotsubscript1.txt :language: python TypeError: XXX missing YYY required positional argument: ZZZ ------------------------------------------------------------ An attempt was made to call the function (indicated by *XXX*) with fewer parameters (indicated by *YYY*) than required. The name of the parameters is indicated by *ZZZ*. **Example** In this example, the ``conv_segons`` function is defined with three parameters but the call is only given two. .. literalinclude:: exemples/missingposargument.py :language: python :linenos: .. literalinclude:: exemples/missingposargument.txt :language: python TypeError: XXX object cannot be interpreted as an YYY ----------------------------------------------------- A value or variable of type *XXX* has been used in a context where type *YYY* is required. **Example** In this example, on line 3, the ``range`` function requires an integer parameter and has been passed a list, instead of the length of the list, which is an integer. .. literalinclude:: exemples/cannotbeinterpreted1.py :language: python :linenos: .. literalinclude:: exemples/cannotbeinterpreted1.txt :language: python TypeError: unhashable type: XXX ------------------------------- An attempt was made to use the data type *XXX* which is not immutable as a key in a dictionary. **Example** In this example, a dictionary is defined where the keys are tuples, but an attempt is made to check if a key exists that is a list, which is a mutable type and gives an error. .. literalinclude:: exemples/unhashabletype1.txt :language: python UnboundLocalError ================= UnboundLocalError: local variable xxx referenced before assignment -------------------------------------------------------------------- Inside a function, an attempt was made to access the value of a variable that has not been defined before. **Example** In this example, no value has been given to the variable com and an attempt has been made to access its value on line 3. .. literalinclude:: exemples/localvarrefbefore1.py :language: python :linenos: .. literalinclude:: exemples/localvarrefbefore1.txt :language: python ValueError ========== ValueError: could not convert XXX to YYY: ZZZ --------------------------------------------- An attempt has been made to convert from type *XXX* to type *YYY* with the value *ZZZ*, but the value does not have a suitable format for the conversion. **Example** In this example, an attempt is made to convert the string s to a real number, but since it contains a comma, the conversion cannot be carried out, since Python uses the dot to denote decimals and the ``float`` function does not know how to interpret the string as a real number. A string containing a dot is then assigned to it and the conversion is performed without problems. .. literalinclude:: exemples/couldnotconvert1.txt :language: python ValueError: invalid literal for int() with base 10: XXX ------------------------------------------------------- An attempt was made to convert the value *XXX* to integer but this value does not have a suitable format in base 10. **Example** In this example the string ``s`` contains a period and therefore the format is incorrect as an integer. Later it is shown how when executing the conversion to float it does work, since the format is correct as a real, or if the string is assigned a value with only integers, the conversion to integer is also executed correctly. .. literalinclude:: exemples/invalidliteralforint1.txt :language: python ValueError: math domain error ----------------------------- An attempt was made to execute a mathematical operation with incorrect data. **Example** In this example, we try to call the function f with the value 0 for the parameter x, which, within the function, tries to calculate the square root of -1 and gives an error because it is a negative number. .. literalinclude:: exemples/mathdomain.py :language: python :linenos: .. literalinclude:: exemples/mathdomain.txt :language: python