Cours8 - LaBRI

publicité
04/11/2009
Algorithmes et
structures de données
Cours 8
Programme
Patrick Reuter
Rappel
Entrées utilisateur
Types : booleén, entier, flottant, chaînes de caractères
Messages d’erreurs Python
Retour sur TD4 et TD5
http://www.labri.fr/~preuter/asd2009
Commentaires
• Commentaires
– Pour augmenter la lisibilité
– Pour le travail en equipe
–…
#Affectations
a = 8
– en python :
b = 3
• précédé par le '#'
• http://inforef.be/swi/download/python_notes.pdf
#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")
afficher(i)
i=1
TANT QUE (i<=5) FAIRE
afficher("Mercredi!")
i=i+1
FIN TANT QUE
afficher("Bonne journée")
afficher(i)
Variables : Types
Type simples:
Type booléen
– Vrai/faux (p.ex. True ou False, boolean)
Type entier
– Nombre entier (p.ex. 5, int)
Mercredi
Bonne journée
2
Mercredi
Mercredi
Mercredi
Mercredi
Mercredi
Bonne journée
6
Type flottant
– Nombre à virgule flottant (p.ex. 5.12, float)
Type chaîne de caractères
– (p.ex. "Johann ", string)
1
04/11/2009
Les entrées du clavier
a = input()
• Démo
affecte la variable a avec une variable de
type entier, flottant, booléen
a = raw_input()
affecte la variable a avec une variable de
type chaîne de caractères.
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"
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 D'INDENTATION:
Le print "fini" devrait commencer en tout début
de ligne
ERREUR DE DECLARATION:
La variable "a" n'est pas déclaré.
2
04/11/2009
Messages d'erreurs python
s = "Demo"
print s[4]
Exercice TD4 : 4.3 et 4.5
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 s n'existe pas. Une
chaîne de caractère avec longueur connaît uniquement les
indices de 0 à 3.
TD 4.3 Correction
TD 4 Correction
• nombre de "i" dans Mississippi
• au moins un "i" dans Mississippi
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
TD 4 Correction
• Palindrom
s = "Mississippi"
longueur = len(s)
resultat = False
i = 0
while (i < longueur):
if (s[i] == "i"):
resultat = True
i = i + 1
– ANNA
– ROTOR
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
3
04/11/2009
TD 4 Correction
• Palindrom
ch[0] == ch[7]
TD 4 Correction
• Palindrom
ch[0] == ch[7]
ch[1] == ch[6]
ch[2] == ch[5]
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
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
• 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]
ch[3] == ch[4]
n = len(ch) - 1
ch[0] == ch[n]
ch[1] == ch[n-1]
ch[2] == ch[n-2]
ch[3]
c
[3] == c
ch[n-3]
[ 3]
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:
if (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"
4
Téléchargement