Introduction
Vous allez développer un programme qui permets l’exécution de certaines tâches. Très fréquemment, un utilisateur doit faire les tâches suivantes :
- Operations arithmétiques (ex. addition des chiffres) ;
- chercher des mots dans un dictionnaire ;
- calculer des statistiques d'un fichier.
Les exercices préparatoires vous permettent de completer certains éléments de cette mission.
Questionnaire de démarrage
Question 1
Considérez le code suivant:
file = open ( "file.txt", "r" )
for line in file:
print ( line.strip ().split (",") )
file.close ()
Le contenu du fichier file.txt est le suivant:
X |
|
, |
X |
|
, |
\n |
|
, |
X |
\n |
X |
X |
, |
, |
X |
Expliquez le résultat de code.
Question 2
You cannot see this exercise because you are currently not logged in.
Click here to log in or get a direct access to the exercice on INGInious by following
this link.
You cannot see this exercise because you are currently not logged in.
Click here to log in or get a direct access to the exercice on INGInious by following
this link.
You cannot see this exercise because you are currently not logged in.
Click here to log in or get a direct access to the exercice on INGInious by following
this link.
You cannot see this exercise because you are currently not logged in.
Click here to log in or get a direct access to the exercice on INGInious by following
this link.
Question 3
You cannot see this exercise because you are currently not logged in.
Click here to log in or get a direct access to the exercice on INGInious by following
this link.
You cannot see this exercise because you are currently not logged in.
Click here to log in or get a direct access to the exercice on INGInious by following
this link.
Question 4
Considérez le code suivant.
command = input ( "Enter your command: " )
parameters = command.split ()
if parameters[0] == "divide":
print ( "The value of your division is: " + str ( float(parameters[1])/float(parameters[2])))
elif parameters[0] == "showfile":
file = open ( parameters[1] )
print ( file.read () )
file.close ()
else:
print ( "Command not recognized")
- Identifiez quelles erreurs pourraient survenir lors de l'exécution de ce code. Il n'est pas nécessaire d'identifier les noms exacts des erreurs en Python; il suffit de décrire les erreurs en français.
You cannot see this exercise because you are currently not logged in.
Click here to log in or get a direct access to the exercice on INGInious by following
this link.
- (Seulement à faire s'il reste du temps.) Modifiez le bloc try ... except pour que les messages soient plus informatifs. Pour cet exercice, vous devez exécuter le code et déclencher les différentes erreurs afin de déterminer leur nom (comme ValueError, FileNotFoundError, ...). Vous trouvez la liste de toutes les différentes exceptions sur https://docs.python.org/3/library/exceptions.html, mais il est plus facile de déterminer leur nom en regardant les messages de Python.
Question 5
Dans la mission, vous devrez lire un fichier qui contient des mots avec leur fréquence d'occurrence. Par exemple:
this,5146
that,10790
these,1575
the,69975
those,864
Identifiez les différentes erreurs que l'on pourrait rencontrer en lisant ce fichier. Il n'est pas nécessaire d'identifier les noms exacts des erreurs; il suffit de les décrire en mots.