04/11/2009
1
Algorithmes et
structures de données
Cours
8
Cours
8
Patrick Reuter
http://www.labri.fr/~preuter/asd2009
Programme
Rappel
Entrées utilisateur
Types : booleén, entier, flottant, chaînes de caractères
Messages d’erreurs Python
Retour sur TD4 et TD5
http://inforef.be/swi/download/python_notes.pdf
Commentaires
• Commentaires
Pour augmenter la lisibilité
– Pour le travail en equipe
– en python :
précédé par le '#'
#Affectations
a = 8
b = 3
#Echange de variables
z = a
a = b
b = z
#Sortie à l'écran
print a,b
Différence SI et TANT QUE
i = 1
SI (i<=5) ALORS
afficher("Mercredi!")
i = i + 1
FIN SI
afficher("Bonne journée")
i = 1
TANT QUE (i<=5) FAIRE
afficher("Mercredi!")
i = i + 1
FIN TANT QUE
afficher("Bonne journée")
afficher(i) afficher(i)
Mercredi
Mercredi
Mercredi
Mercredi
Mercredi
Bonne journée
6
Mercredi
Bonne journée
2
Variables : Types
Type simples:
Type booléen
Vrai/faux (p.ex. True ou False, boolean)
Type entie
r
Nombre entier (p.ex. 5, int)
Type flottant
Nombre à virgule flottant (p.ex. 5.12, float)
Type chaîne de caractères
– (p.ex. "Johann ", string)
04/11/2009
2
Les entrées du clavier
a = input()
affecte la variable aavec une variable de
type entier, flottant, booléen
a = raw_input()
affecte la variable aavec une variable de
type chaîne de caractères.
•Démo
Messages d'erreurs python
Messages d'erreurs python
z = 1
prinnt z
ERREUR DE SYNTAXE : Il faut écrire print au lieu de prinnt
Messages d'erreurs python
z = 1
if z==1:
print z
print "fini"
ERREUR D'INDENTATION:
Le print "fini" devrait commencer en tout début
de ligne
Messages d'erreurs python
z = a
Traceback (most recent call last):
File "D:/preuter/stud/asd2008/td/td7machine/erreurs.py", line 1, in <module>
z = a
NameError: name 'a' is not defined
ERREUR DE DECLARATION:
La variable "a" n'est pas déclaré.
04/11/2009
3
Messages d'erreurs python
s = "Demo"
print s[4]
Traceback (most recent call last):
File "D:\preuter\stud\asd2008\td\td7machine\erreurs.py", line 2, in <module>
print s[4]
IndexError: string index out of range
ERREUR DE BORNES:
L'index 4 dans la chaîne de caractères sn'existe pas. Une
chaîne de caractère avec longueur connaît uniquement les
indices de 0 à 3.
Exercice TD4 : 4.3 et 4.5
TD 4.3 Correction
nombre de "i" dans Mississippi
au moins un "i" dans Mississippi
TD 4 Correction
s
= "Mississippi"
longueur = len(s)
compteur = 0
i = 0
while (i < longueur):
if (s[i] == "i"):
compteur = compteur + 1
i = i + 1
print "nombre de 'i' dans ",s," : ",compteur
TD 4 Correction
s = "Mississippi"
longueur = len(s)
resultat = False
i0
i
=
0
while (i < longueur):
if (s[i] == "i"):
resultat = True
i = i + 1
if resultat == True:
print "Oui, il y a au moins un ‘i’ dans", s
else: print "Non, il n’y a pas de ‘i’ dans ",s
TD 4 Correction
• Palindrom
– ANNA
ROTOR
ROTOR
04/11/2009
4
TD 4 Correction
• Palindrom ch[0] == ch[7]
TD 4 Correction
• Palindrom ch[0] == ch[7]
ch[1] == ch[6]
TD 4 Correction
• Palindrom ch[0] == ch[7]
ch[1] == ch[6]
ch[2] == ch[5]
TD 4 Correction
• Palindrom ch[0] == ch[7]
ch[1] == ch[6]
ch[2] == ch[5]
ch
==
ch
[4]
n = len(ch) - 1
ch[0] == ch[n]
ch[1] == ch[n-1]
ch[2] == ch[n-2]
c
h
==
c
h
[
n-
3]
ch
ch
[4]
c
c
[
3]
TD 4.3 Correction
i = 0
n = len(s) - 1
resultat = VRAI
TANT QUE i <= n/2 FAIRE
SI s[i] != s[n-i] ALORS
resultat = FAUX
FIN SI
FIN
SI
i = i + 1
FIN TANT QUE
SI resultat == VRAI ALORS
afficher("C'est un palindrome");
SINON
afficher("Ce n'est pas un palindrome");
FIN SI
TD 4 Correction
print "entrer la chaine de caractere à tester"
s = raw_input()
i =0
n = len(s) - 1
resultat = True
while i <= n/2:
i
f
(
s[i] != s[n
-
i
]):
resultat = False
i = i + 1
if (resultat == True):
print "C'est un palindrome"
else:
print "Ce n'est pas un palindrome"
1 / 4 100%