2021-2022/2nd/13_Programmation/3E_conditions.ipynb

261 lines
5.9 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"id": "59986333",
"metadata": {},
"source": [
"# Conditions\n",
"\n",
"Quand on veut gérer des situations différentes, on utiliser une structure `if`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "90f79825",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Choisir un nombre (a): 2\n",
"Choisir un autre nombre(b): 4\n",
"a est plus petit que b\n"
]
}
],
"source": [
"a = str(input(\"Choisir un nombre (a): \"))\n",
"b = str(input(\"Choisir un autre nombre(b): \"))\n",
"\n",
"if a < 10: # Si\n",
" print(\"a est plus petit que 10\")\n",
"elif a == b: # Sinon si\n",
" print(\"a est égal à b\")\n",
"else: # Sinon\n",
" print(\"J'ai rien à dire\")"
]
},
{
"cell_type": "markdown",
"id": "6e39d9d8",
"metadata": {},
"source": [
"## Exercices"
]
},
{
"cell_type": "markdown",
"id": "21e86bf8",
"metadata": {},
"source": [
"1. Un musée accorde des tarifs réduits aux groupes contenant au minimum 6 personnes de plus de 18 ans. Un informaticien du musée veut écrire un programme Python pour les clients qui achètent des billets en ligne. Ce programme doit demander au client le nombre dadultes de plus de 18 ans du groupe puis afficher, suivant la réponse du client, lune des deux phrases suivantes :\n",
"\n",
"- Vous avez droit au tarif Groupe !\n",
"- Vous navez pas droit au tarif Groupe !\n",
" \n",
"Linformaticien a commencé à écrire le programme ci-dessous. Complétez-le :"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f719d9ac",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Combien ...12\n"
]
}
],
"source": [
"nombre_adulte = int(input(\"Combien ...\"))\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "bd8f17d8",
"metadata": {},
"source": [
"2. À l'achat d'une voiture neuve, l'état applique un malus écologique. C'est une taxe qui dépend de la quantité de CO2 rejeté par le véhicule. Voici quelques montants de cette taxe\n",
"\n",
"| Emission de C02 (g/km) | Montant du malus 2022 |\n",
"|------------------------|-----------------------|\n",
"| Moins de 127 | 0 |\n",
"| De 128 à 151 | 1074 |\n",
"| De 152 à 200 | 18 188 |\n",
"| De 201 à 224 | 36 447 |\n",
"| Plus de 225 | 40 000 |\n",
"\n",
"Écrire un programme qui demande l'emission du véhicule et qui affiche le montant du malus."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e07500c9",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "56711adc",
"metadata": {},
"source": [
"3. **Questions flashs et correction automatique**\n",
"\n",
"Préparer une série de 4 questions flashs simples où la réponse est un nombre ou un mot. Écrire un programme qui demande une réponse aux questions flashs et qui dit si oui ou non la réponse est juste."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "01e13ca3",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "7e24624a",
"metadata": {},
"source": [
"# Comparaison et logique"
]
},
{
"cell_type": "markdown",
"id": "a140ea9c",
"metadata": {},
"source": [
"En anglais, `True` signifie `vrai` et `False` signifie `faux`.\n",
"\n",
"Quelques opérateurs pour faire des comparaison entre nombres"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b60a11dd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n",
"False\n",
"True\n",
"True\n",
"True\n",
"True\n",
"False\n",
"False\n"
]
}
],
"source": [
"print(2 == 2)\n",
"print(2 == 3)\n",
"print(2 != 2)\n",
"print(10 != 5)\n",
"print(4 < 5)\n",
"print(4 <= 4)\n",
"print(34 > 2)\n",
"print(34 > 50)\n",
"print(5 >= 10)"
]
},
{
"cell_type": "markdown",
"id": "a801961d",
"metadata": {},
"source": [
"Faire la liste des opérateurs et écrire une phrase pour décrire ce qu'ils signifie."
]
},
{
"cell_type": "raw",
"id": "7706dd5d",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "a4a0f415",
"metadata": {},
"source": [
"Pour combiner plusieur condition, on peut utiliser les mots clés `not` (pas), `and` (et) et `or` (ou)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f3512da",
"metadata": {},
"outputs": [],
"source": [
"print(2 == 2 and 4 == 4)\n",
"print(2 == )"
]
},
{
"cell_type": "markdown",
"id": "acee5b14",
"metadata": {},
"source": []
},
{
"cell_type": "raw",
"id": "565d9da6",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "4f4b6948",
"metadata": {},
"source": [
"## Exercices"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2aad72c0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}