2022-2023/1NSI/04_Fonctions_specifications_et_tests/2E fonction documentations.ipynb

359 lines
7.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "ebadd52b",
"metadata": {},
"source": [
"# Fonctions et documentations\n",
"\n",
"Dans le TP précédent, vous avez écrit des fonctions qui marchent. C'est un bon début, mais il est important pour soit et pour les autres de documenter et de spécifier son code pour le rendre plus explicite et éviter les bugs.\n",
"\n",
"Voici une fonction correctement documentée"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4f5978cc",
"metadata": {},
"outputs": [],
"source": [
"def entoure(texte:str, symbole:str, nombre:int)->str:\n",
" \"\"\" Entoure le texte avec le symbole (nombre de fois)\n",
" \n",
" :param texte(str): texte à entourer\n",
" :param symbole(str): symbole pour entourer\n",
" :param nombre(int): nombre de répétition du symbole (avant et après)\n",
" :return (str): le texte entouré\n",
" \n",
" :example:\n",
" >>> entoure(\"NSI\", \"~\", 3)\n",
" ~~~NSI~~~\n",
" >>> entoure(\"Plop\", \"-\", 1)\n",
" -Plop-\n",
" \"\"\"\n",
" symbole_repete = nombre*symbole\n",
" return symbole_repete + texte + symbole_repete"
]
},
{
"cell_type": "markdown",
"id": "1ce40493",
"metadata": {},
"source": [
"1. Repérer les nouveaux élèments et la syntaxe associée qui permettent de documenter la fonction."
]
},
{
"cell_type": "markdown",
"id": "3a50223f",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "3115b6ce",
"metadata": {},
"source": [
"## Carré\n",
"\n",
"Programmer une fonction qui prend en argument un nombre et qui retourne son carré.\n",
"\n",
"Fonction"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "363b80f7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "988eec99",
"metadata": {},
"source": [
"Tests"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7331c1eb",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "53bd8be0",
"metadata": {},
"source": [
"## Max à 2\n",
"\n",
"Programmer une fonction qui prend en argument deux nombres puis qui renvoie le plus grand.\n",
"\n",
"Fonction"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77541d90",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "ffece552",
"metadata": {},
"source": [
"Tests"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "79f0e4d4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "5171aad2",
"metadata": {},
"source": [
"## Max à 3\n",
"\n",
"Programmer une fonction qui prend en argument 3 nombres et qui renvoie le plus grand.\n",
"\n",
"Fonctions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2812d74e",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "981bd705",
"metadata": {},
"source": [
"Tests"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f124ff62",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "6d76f12c",
"metadata": {},
"source": [
"Essayer de programmer cette fonction en réutilisant la fonction max à 2 de la question précédente"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1dd297be",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "bd596431",
"metadata": {},
"source": [
"## Question réponse\n",
"\n",
"Programmer une fonction qui prend en argument une question et une réponse qui pose la question à l'utilisateur puis qui renvoie True si la réponse est juste ou False sinon.\n",
"\n",
"Fonction"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1446ed52",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "101dfbfb",
"metadata": {},
"source": [
"Tests"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9190ce3b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "104c2a6c",
"metadata": {},
"source": [
"## Mention\n",
"\n",
"Dans cette partie, vous devez programmer une fonction qui prend en argument une note (entre 0 et 20) puis qui retourne la mention associée suivant la règle:\n",
"\n",
"- R si la note < 10\n",
"- A si 10 <= note < 12\n",
"- AB si 12 <= note < 14\n",
"- B si 14 <= note < 16\n",
"- TB si 16 <= note\n",
"\n",
"Fonction\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6757393e",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "c09ad023",
"metadata": {},
"source": [
"Tests"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e064533a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "ef55d84d",
"metadata": {},
"source": [
"### Questionnaire\n",
"\n",
"Ecrire un programme qui pose 10 questions, fait gagner 2points par question juste puis qui donne une mention à l'utilisateur. La mention devra être encadré avec le symbole \"o\"\n",
"\n",
"Il n'est pas attendu que vous mettiez tout ce programme dans une seul fonction. Par contre, vous devrez réutiliser le plus possible de fonctions déjà programmée précédement et celle ci-dessous"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "12e09ca3",
"metadata": {},
"outputs": [],
"source": [
"def encadre(texte:str, symbole:str)->str:\n",
" \"\"\" Encadre le texte avec le symbole\n",
" \n",
" :param texte(str): texte à encadrer\n",
" :param symbole(str): symbole pour encadrer\n",
" :return(str): le texte encadré\n",
" \n",
" \n",
" :example:\n",
" >>> encadre(\"A\", \"~\")\n",
" ~~~\n",
" ~A~\n",
" ~~~\n",
" >>> encadre(\"ZE\", \"-\")\n",
" ----\n",
" -ZE-\n",
" ----\n",
" \n",
" \"\"\"\n",
" texte_encadre = entoure(texte, symbole, 1)\n",
" longueur_boite = len(texte_encadre)\n",
" haut_bas = longueur_boite * symbole\n",
" return haut_bas + \"\\n\" + texte_encadre + \"\\n\" + haut_bas"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "7afc6232",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"----\n",
"-ZE-\n",
"----\n"
]
}
],
"source": [
"print(encadre(\"ZE\", \"-\"))"
]
},
{
"cell_type": "markdown",
"id": "4976f3cd",
"metadata": {},
"source": [
"Votre programme"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25466c67",
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}