Doc: Comprendre les messages d'erreur
Messages d'erreur
Съдържание
1. NameError: name 'blablaBla' is not defined [редактиране]
2. SyntaxError: invalid syntax [редактиране]
3. SyntaxError: expected an indented block [редактиране]
4. SyntaxError: unindent does not match any outer indentation level [редактиране]
5. SyntaxError: unexpected indent [редактиране]
6. SyntaxError: can't assign to operator [редактиране]
7. SyntaxError: can't assign to function call [редактиране]
8. SyntaxError: unexpected EOF while parsing [редактиране]
9. SyntaxError: invalid character in identifier [редактиране]
10. IndentationError: unindent does not match any outer indentation level [редактиране]
11. ValueError: math domain error [редактиране]
12. TypeError: 'list' object cannot be interpreted as an integer [редактиране]
13. TypeError: f() takes 2 positional arguments but 3 were given [редактиране]
14. TypeError: f() missing 1 required positional argument: 'y' [редактиране]
15. TypeError: 'int' object is not iterable [редактиране]
16. TypeError: 'int' object is not callable [редактиране]
17. IndexError: list index out of range [редактиране]
18. TypeError: unorderable types: __c_node() <= int() [редактиране]
19. TypeError: 'function' object is not subscriptable [редактиране]
20. TypeError: unsupported operand type(s) for *: 'function' and 'function' [редактиране]
21. IndexError: image index out of range [редактиране]
22. ImportError: No module named 'bibimages' [редактиране]
23. FileNotFoundError: [Errno 2] No such file or directory: 'tgv2005.dot' [редактиране]
24. UnboundLocalError: local variable 'n' referenced before assignment [редактиране]
NameError: name 'blablaBla' is not defined [редактиране]
Erreur : le nom 'blablabla' n'est pas défini. Soit vous utilisez la variable (ou la fonction) blablabla avant de la définir soit vous vous êtes trompé en tapant le nom de la variable (ou de la fonction) blablabla. Attention Python est sensible à l'utilisation
des majuscules. Il peut aussi arriver qu'on ait oublié des guillemets autour d'une chaîne de caractères (voir exemple 3):
Exemple 1:
def blablaBla(x):
a=2*x
return x*a
y = blablabla(2)
NameError: name 'blablabla' is not defined
Ici nous avons mal orthographié 'blablabla' : il y un B majuscule dans le 3ième bla de la définition de la fonction et on a oublié la majuscule dans l'appel de la fonction.
j = 5
x = 2 * i
i = 4
NameError: name 'i' is not defined
Origine du problème : on utilise le nom 'i' dans cette expression alors qu'il n'a jamais été défini avant. Soit on aurait dû initialiser la variable 'i' avant (déplacer l'instruction i=4 avant l'instruction x = 2 * i) soit on a mal orthographié la variable (on voulait taper 'j' au lieu de 'i').
Exemple un peu plus subtil :
def f(x):
m = x*x
return m
f(5)
y = m
NameError: name 'm' is not defined
Origine du problème : la variable 'm' est une variable locale à la fonction 'f', elle ne "vit" qu'à l'intérieur de la fonction 'f'.
Exemple 3 :
monImage = ouvrirImage(teapot.png)
Solution :
monImage = ouvrirImage("teapot.png")Explication : comme on a oublié les guillemets, python pense que teapot est le nom d'une variable... qui n'est donc pas définie.
SyntaxError: invalid syntax [редактиране]
Erreur : erreur de syntaxe. Cela veut dire que l'interpréteur est un peu perdu car il n'arrive pas à déchiffrer votre programme. L'origine du problème n'est pas forcément à l'endroit pointé par l'interpréteur, comme nous allons le voir dans les
exemples ci-dessous.
exemple 1 :
def f(x)
^
SyntaxError: invalid syntax
Origine du problème : on a oublié les ':' à la fin de la ligne
solution :
def f(x):
if i = 6:problème : après le if il s'attend à avoir une expression booléen. Ici il se retrouve avec une affectation, il est donc perdu.
^
if i == 6:
exemple 3 :
if i == 9
^
SyntaxError: invalid syntax
Origine du problème : on a oublié les ':' à la fin de la ligne
solution :if i==9 :
def f(x):
return sqrt((x*x - x)
print(f(5))
^
def f(x):
return sqrt((x*x - x))
print(f(5))
def f(x):en Python 3 (contrairement à python 2) print est une fonction comme les autres. Donc pour appeler cette fonction il faut mettre des parenthèses.
return sqrt((x*x - x)
print f(5)
^
def f(x):
return sqrt((x*x - x)
print (f(5))
^
SyntaxError: expected an indented block [редактиране]
Erreur : problème d'indentation. Le plus fréquemment il manque une ou plusieurs tabulation à l'endroit pointé par le message d'erreur (voir exemple 1). Parfois, l'origine du problème vient de plus haut dans votre le fichier. Par exemple lorsqu'on a commencé
une fonction, un if ou un for, que l'on n'a pas fait suivre par une instruction (voir exemple 2) ou qu'on a commenté l'instruction en question (exemple 3).
Exemple 1 :
if i == 9:
toto = 9
Solution 1 :
if i == 9 :
toto = 9
Exemple 2 : vous avez déclaré une fonction sans définir son code :
def f(x):
y = 5*2
Solution 2: rajouter un return dans votre fonction
def f(x):
return
y = 5*2
Exemple 3 : vous avez commenté qu'une partie du code :
for i in range(5):
## print(i)
y = 5*2
Solution 2: mettre
##for i in range(5):
## print(i)
y = 5*2
SyntaxError: unindent does not match any outer indentation level [редактиране]
if i == 9:SyntaxError: unindent does not match any outer indentation level
titi = 5
toto = 4
^
Erreur: le niveau d'indentation (nombre de tabulation) ne correspond à aucun niveau d'indentation externe.
ici : l'instruction 'toto = 9' est soit trop à gauche soit trop à droite. Cela arrive souvent lorsqu'on mélange les tabulations et les 'espaces'.
Solution
if i == 9:
titi = 5
toto = 4
ou (selon le contexte)
if i == 9:
titi = 5
toto = 4
SyntaxError: unexpected indent [редактиране]
Problème: Il y a un niveau d'indentation de trop (il y a trop de tabulations sur cette ligne).
Exemple :
def f(x):
y = x*x
return y
Solution : supprimer les tabulations superflux
def f(x):
y = x*x
return y
SyntaxError: can't assign to operator [редактиране]
i+1 = 5
^
SyntaxError: can't assign to operator
Erreur : on ne peut pas assigner une valeur à un opérateur (ici le symbole '+').
Origine du problème : à gauche du symbole '=' (symbole d'affectation) il ne peut y avoir qu'un nom de variable (déjà définie ou non).
Ici on voulait peut-être écrire i = 5 - 1
SyntaxError: can't assign to function call [редактиране]
def f(x):^
return x*x
f(5) = y
SyntaxError: can't assign to function call
Erreur = on ne peut pas assigner une valeur à un appel de fonction.
def f(x):
return x*x
y = f(5)
SyntaxError: unexpected EOF while parsing [редактиране]
def f( n ):
s = 0
for i in range( n ):
### fin du fichier ####
def f( n ):
s = 0
for i in range( n ):
s = s + i
return s
### fin du fichier ####
### fin du fichier ####
Solution : fermer toutes les parenthèses ouvertes.
### fin du fichier ####
SyntaxError: invalid character in identifier [редактиране]
Vous avez tapé un caractère non "standard" : exemple "²"
IndentationError: unindent does not match any outer indentation level [редактиране]
def f(x):
nb = 0
for i in range(10):
nb = nb +1
return nb
ValueError: math domain error [редактиране]
y = -5
x = sqrt( y )
TypeError: 'list' object cannot be interpreted as an integer [редактиране]
L=[5,2,3]
for i in range(L):
Ici on passe une liste en paramètre à la fonction range() alors qu'elle s'attend à avoir un entier.
Solution : ne pas utiliser la fonction range, mais directement la liste
L=[5,2,3]
for i in L:
TypeError: f() takes 2 positional arguments but 3 were given [редактиране]
def f(x,y):
return x + y
print(f(5,6,7))
Solution : appeler la fonction avec le bon nombre d'arguments
TypeError: f() missing 1 required positional argument: 'y' [редактиране]
Problème: on appelle la fonction 'f()' en ne lui passant pas assez d'arguments
Exemple :
def f(x,y):
return x + y
print(f(5))
Solution : appeler la fonction avec le bon nombre d'arguments
TypeError: 'int' object is not iterable [редактиране]
Exemple 1:
n = 5
for i in n:
print(i)
n = 5
for i in range( n ):
print(i)
TypeError: 'int' object is not callable [редактиране]
def f(x):
return x*x
y = 2
z = y(5)
def f(x):
return x*x
y = 2
z = f(5)
monImage = nouvelleImage(300,200)
for y in range(largeurImage(monImage)):
colorierPixel(monImage,0,y(255,255,255))
monImage = nouvelleImage(300,200)
for y in range(largeurImage(monImage)):
colorierPixel(monImage,0,y,(255,255,255))
IndexError: list index out of range [редактиране]
L = [34,54,23]
s = 0
for x in range(1,4):
s = s + L[x]
for x in range(0,3):
s = s + L[x]
for x in range(len(L)):
s = s + L[x]
TypeError: unorderable types: __c_node() <= int() [редактиране]
c = 5
for s in listeSommets(G):
if s <= c:
c = 5
for s in listeSommets(G):
if degre(s) <= c:
TypeError: 'function' object is not subscriptable [редактиране]
def moyenneListe(L):
(...)
print(moyennesListe[2,6,3,5])
Solution ajouter des parenthèses autour des crochets :
print(moyennesListe([2,6,3,5]))
TypeError: unsupported operand type(s) for *: 'function' and 'function' [редактиране]
def f(x):
return x *x
def g(y):
return f*f
def f(x):
return x *x
def g(y):
return f(y)*f(y)
IndexError: image index out of range [редактиране]
monImage = nouvelleImage(300,200)
colorierPixel(monImage,300,200,(255,255,255))
ImportError: No module named 'bibimages' [редактиране]
from bibimages import *
Problème : python ne trouve pas le fichier bibimages.py. Il y a 2 causes principales à ce problème :
1) Soit vous avez mal tapé bibimages dans votre fichier.
2) Soit vous n'avez pas encore récupéré le fichier bibimages.py sur la page http://dept-info.labri.fr/ENSEIGNEMENT/INITINFO/initinfo/support.html
3) Soit vous l'avez bien récupéré, mais vous l'avez mis dans le mauvais autre répertoire.
4) Soit vous l'avez mis au bon endroit, mais vous l'avez appelé autrement que exactement bibimages.py (attention par défaut Window cache les extensions de nom de fichiers !)
FileNotFoundError: [Errno 2] No such file or directory: 'tgv2005.dot' [редактиране]
Le fichier 'tgv2005.dot' n'est pas présent dans le répertoire courant. Soit vous n'avez pas téléchargé ce fichier, soit vous l'avez téléchargé dans un autre répertoire. => Vérifiez le contenu du répertoire courant.
UnboundLocalError: local variable 'n' referenced before assignment [редактиране]
Vous utilisez une variable avant de l'avoir initialisée.
Autre message d'Erreur [редактиране]
python TypeError: 'NoneType' object does not support item assignment