Python au lycée (4) : Les fonctions
Exercices
Python : Calculer l’indice de masse corporelle
10 minutes
Votre progression
Créez un compte gratuit pour suivre votre avancement et reprendre où vous avez laissé.
Créer un compteObjectifs travaillés
L'indice de masse corporelle (IMC) d'une personne est défini par la formule :
$ \text{IMC} = \dfrac{m}{t^2} $
où $ m $ désigne la masse en kilogrammes et $ t $ la taille en mètres.
- Calculer « à la main » l'IMC d'une personne mesurant $ 1{,}75 $ m et pesant $ 70 $ kg. On arrondira le résultat à $ 0{,}1 $ près.
Écrire une fonction Python imc qui prend en arguments la masse $ m $ et la taille $ t $, et qui renvoie l'IMC arrondi à $ 0{,}1 $ près.
- Indication : l'instruction round(x, 1) arrondit le nombre $ x $ à $ 0{,}1 $ près.
Utiliser la fonction pour calculer l'IMC des personnes suivantes :
- Alice : $ 60 $ kg pour $ 1{,}67 $ m ;
- Bastien : $ 85 $ kg pour $ 1{,}80 $ m.
Corrigé
- On calcule $ 1{,}75^2 = 3{,}0625 $, puis $ \dfrac{70}{3{,}0625} \approx 22{,}857 $. Arrondi à $ 0{,}1 $ près, on obtient un IMC de $\mathbf{22{,}9}$.
La fonction prend deux arguments :
def imc(m, t): return round(m / t**2, 1)- Pour Alice : $ \text{imc}(60, 1.67) = \text{round}(60 / 1{,}67^2 ; 1) $. On calcule $ 1{,}67^2 = 2{,}7889 $, puis $ 60 \div 2{,}7889 \approx 21{,}513 $, arrondi à $\mathbf{21{,}5}$.
- Pour Bastien : $ \text{imc}(85, 1.80) $. On calcule $ 1{,}80^2 = 3{,}24 $, puis $ 85 \div 3{,}24 \approx 26{,}234 $, arrondi à $\mathbf{26{,}2}$.
Remarque
En Python, on écrit la puissance avec l'opérateur **. L'instruction t**2 correspond donc à $ t^2 $.