Find the position · Trouver la position
Find the position
Search a list and report where something is, counting from 0.
Loop over the positions with range(len(items)), so you have the index i as well as the item items[i]. return leaves the function straight away, which is exactly what you want at the first match. Return -1 only after the loop has checked every item.
Trouver la position
Recherchez dans une liste et indiquez où se trouve un élément, en comptant à partir de 0.
Parcourez les positions avec range(len(items)), afin d'avoir l'index i ainsi que l'élément items[i]. return quitte immédiatement la fonction, ce qui est exactement ce que vous voulez au premier match. Retournez -1 uniquement après que la boucle ait vérifié tous les éléments.
Write find_index(items, target) that returns the position of the first · premier target in items, or -1 if it is not there. Do not use .index(). · Écrivez find_index(items, target) qui retourne la position du premier target dans items, ou -1 s'il n'est pas là. N'utilisez pas .index().
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.