176 lines
3.9 KiB
Plaintext
176 lines
3.9 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Python et fonctions"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Que fait ce programme?\n",
|
||
|
"\n",
|
||
|
"* Copier puis coller le programme, l'executer. Fait-il ce que vous attendiez de lui?\n",
|
||
|
"* Commenter au dessus de chaque ligne (avec #...) pour expliquer ce qu'elle fait."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"==== Menu du jour ====\n",
|
||
|
"\n",
|
||
|
"Entrées -----\n",
|
||
|
" * Choux\n",
|
||
|
" * Salade\n",
|
||
|
"Plats -------\n",
|
||
|
" * Frites\n",
|
||
|
" * Haricots vert\n",
|
||
|
"\n",
|
||
|
"Faites votre choix\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"entree1 = 'Choux'\n",
|
||
|
"entree2 = 'Salade'\n",
|
||
|
"plat1 = 'Frites'\n",
|
||
|
"plat2 = 'Haricots vert'\n",
|
||
|
"print(\"==== Menu du jour ====\")\n",
|
||
|
"print(\"\")\n",
|
||
|
"print(\"Entrées -----\")\n",
|
||
|
"print(\" * \", entree1)\n",
|
||
|
"print(\" * \", entree2)\n",
|
||
|
"print(\"Plats -------\")\n",
|
||
|
"print(\" * \", plat1)\n",
|
||
|
"print(\" * \", plat2)\n",
|
||
|
"print(\"\")\n",
|
||
|
"print(\"Faites votre choix\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"* Ajouter un choix de dessert à ce programme"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Moins se répéter\n",
|
||
|
"\n",
|
||
|
"Dans le programme précédent, on répète plusieurs fois comment il faut afficher un choix.\n",
|
||
|
"\n",
|
||
|
" print(\" * \", entree1)\n",
|
||
|
" print(\" * \", entree2)\n",
|
||
|
"\n",
|
||
|
"C'est peu long à écrire mais surtout si l'on veut mettre un `~` au lieu d'une `*`, il faut changer chaque ligne (et donc surement en oublier!). Pour éviter d'avoir à réécrire chaque fois la même chose, on peut définir des fonctions.\n",
|
||
|
"\n",
|
||
|
"Ici notre fonction va transformer le nom d'un plat en ligne de notre menu."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def presente(plat):\n",
|
||
|
" return ' ~ '+ plat"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"On peut alors utiliser cette fonction pour présenter notre menu."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"==== Menu du jour ====\n",
|
||
|
"\n",
|
||
|
"Entrées -----\n",
|
||
|
" ~ Choux\n",
|
||
|
" ~ Salade\n",
|
||
|
"Plats -------\n",
|
||
|
" * Frites\n",
|
||
|
" * Haricots vert\n",
|
||
|
"\n",
|
||
|
"Faites votre choix\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"entree1 = 'Choux'\n",
|
||
|
"entree2 = 'Salade'\n",
|
||
|
"plat1 = 'Frites'\n",
|
||
|
"plat2 = 'Haricots vert'\n",
|
||
|
"print(\"==== Menu du jour ====\")\n",
|
||
|
"print(\"\")\n",
|
||
|
"print(\"Entrées -----\")\n",
|
||
|
"print(presente(entree1))\n",
|
||
|
"print(presente(entree2))\n",
|
||
|
"print(\"Plats -------\")\n",
|
||
|
"print(\" * \", plat1)\n",
|
||
|
"print(\" * \", plat2)\n",
|
||
|
"print(\"\")\n",
|
||
|
"print(\"Faites votre choix\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"* Terminer de modifier le programme pour utiliser la fonction.\n",
|
||
|
"* Modifier la présentation d'un plat"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3",
|
||
|
"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.7.1"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 2
|
||
|
}
|