Solution Exercice 10: algorithme python qui renvoie la position d'un élément dans une liste
Exercice 10 Ecrire un algorithme en python sous forme de fonction qui prends en paramètre un couple (L, a) formé d'une liste L et d'un élément a et qui renvoie la position de l'élément a dans la liste L. La fonction doit renvoyer -1 si l'élément a n'est pas présent dans la liste. Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#coding: utf-8 def examinList(L, a): if a in L: return L.index(a) else: return -1 # Exemple L = [13, 9 , 5 , 23] a = 9 b = 11 print(examinList(L,a)) # affiche 1 print(examinList(L,b)) # affiche -1 |
…