Tous les exemples

Exemples

Trouver l’erreur dans deux lignes de Python

Une boucle for sans deux-points : l’erreur repérée, pourquoi Python s’arrête avant d’afficher quoi que ce soit, puis la ligne corrigée.

  • 0:45
  • Problèmes de code
  • Narrée par Erinome
  • Anglais

Créée à partir d’un prompt

Find the error: for i in range(5) print(i)

Transcription de la vidéo

In this video, we find and fix a Python syntax error.

Question one. Find the error: The first line reads for i in range, with five in parentheses. The indented second line reads print, with i in parentheses.

Notice that line one is missing a colon after the closing parenthesis. Python requires a colon at the end of a for loop header. Without it, Python raises a SyntaxError before either line executes, so nothing is printed.

The indentation of print is correct and does not need changing. Hence, the corrected first line is for i in range, open parenthesis, five, close parenthesis, colon.

Votre sujet, dessiné en une minute environ.

Vos 2 premières vidéos sont gratuites, jusqu’à 1 minute chacune, avec filigrane. Sans carte bancaire. Tout le produit, toutes les voix et tous les styles compris.