SNT Python Exos

Identification

Infoforall

10 - Exercice Python


01° On vous donne le programme suivant : donner la succession de lignes de code suivi par Python et le résultat final de x et x2.

1 2 3 4 5 6 7 8 9 10 11
def transfert(a, b): if a > b: reponse = a else: reponse = b return reponse x = transfert(20, 50) print(x) x2 = transfert(40, 10) print(x2)

...CORRECTION...

L1(définition)

L8(appel avec un envoi de 20 et 50) - L1(a=20, b=50) - L2(False) - L4 - L5(reponse=50) - L6 - L8(retour x=50)

L9

L10(appel avec un envoi de 40 et 10) - L1(a=40, b=10) - L2(True) - L3(reponse=40) - L6 - L10(retour x2=40)

L11

02° Compléter le programme ci-dessous : on doit pouvoir envoyer un string stocké dans texte à la fonction, ainsi qu'un entier stocké dans choix.

Si l'entier est 0, c'est qu'on veut que la fonction renvoie un simple élément HTML paragraphe p.

Si l'entier est 1, c'est qu'on veut que la fonction renvoie un élément HTML titre h2.

Si l'entier est 2, c'est qu'on veut que la fonction renvoie un élément HTML image img.

Exemple d'utilisation :

>>> fournir_html("Bonjour", 0) '<p>Bonjour</p>' >>> fournir_html("Bonjour", 1) '<h1>Bonjour</h1>' >>> fournir_html("mon_image.png", 2) '<img src="mon_image.png">'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
def fournir_html(texte, choix): code_html = "" if choix == ...: code_html = "<p>" + texte + "</p>" elif ... code_html = "<h1>" + ... elif ... code_html = "<img src='" + ... + "' >" return ... code = fournir_html("Bonjour", 1) print(code) code2 = fournir_html("photo.png", 2) print(code2)

...CORRECTION...

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
def fournir_html(texte, choix): code_html = "" if choix == 0: code_html = "<p>" + texte + "</p>" elif choix == 1: code_html = "<h1>" + texte + "</h1>" elif choix == 2: code_html = "<img src='" + texte + "' >" return code_html code = fournir_html("Bonjour", 1) print(code) code2 = fournir_html("photo.png", 2) print(code2)

Activité publiée le 03 03 2025
Dernière modification : 03 03 2025
Auteur : ows. h.