# Aide - TP08 McDonald's ## Rappels utiles ### Structure du menu (dictionnaire imbrique) ```python # menu = {categorie: {article: {infos}}} menu = { "burgers": { "Big Mac": {"prix": 5.50, "calories": 540, "temps_prep": 3}, "McChicken": {"prix": 4.90, "calories": 420, "temps_prep": 3} }, "accompagnements": {...}, "boissons": {...} } # Acceder au prix du Big Mac prix = menu["burgers"]["Big Mac"]["prix"] # Parcourir toutes les categories for categorie, articles in menu.items(): for nom_article, infos in articles.items(): print(nom_article, ":", infos["prix"], "euros") ``` ### Structure des commandes ```python commande = { "id": 101, "items": [("Big Mac", 2), ("Frites (G)", 2)], # (article, quantite) "heure": "12:05" } # Parcourir les items for article, quantite in commande["items"]: print(quantite, "x", article) ``` ### Trouver un article dans le menu ```python def trouver_article(menu, nom): for categorie, articles in menu.items(): if nom in articles: return articles[nom] return None ``` --- ## Exercice 1 ### 1.1 - Affichage du menu ```python def afficher_menu(menu, categorie): if categorie in menu: print("===", categorie.upper(), "===") for nom, infos in menu[categorie].items(): print(" ", nom, ":", infos["prix"], "euros (", infos["calories"], "cal)") ``` ### 1.2 - Recherche d'articles ```python def rechercher_article(menu, nom_recherche): resultats = [] nom_lower = nom_recherche.lower() for categorie, articles in menu.items(): for nom_article, infos in articles.items(): if nom_lower in nom_article.lower(): resultats.append({ "nom": nom_article, "categorie": categorie, "prix": infos["prix"], "calories": infos["calories"] }) return resultats ``` ### 1.3 - Statistiques du menu ```python def stats_menu(menu): tous_articles = [] for categorie, articles in menu.items(): for nom, infos in articles.items(): tous_articles.append({"nom": nom, "infos": infos}) # Prix moyen total_prix = 0 for article in tous_articles: total_prix = total_prix + article["infos"]["prix"] prix_moyen = total_prix / len(tous_articles) # Article le moins calorique min_cal = tous_articles[0] for article in tous_articles: if article["infos"]["calories"] < min_cal["infos"]["calories"]: min_cal = article return { "prix_moyen": round(prix_moyen, 2), "article_moins_calorique": min_cal["nom"] } ``` --- ## Exercice 2 ### 2.1 - Calculer le prix ```python def calculer_prix(commande, menu, menus_bestof): total = 0 for article, qte in commande["items"]: # Verifier si c'est un Best-Of if article in menus_bestof: total = total + menus_bestof[article]["prix"] * qte else: # Chercher dans le menu normal for categorie, articles in menu.items(): if article in articles: total = total + articles[article]["prix"] * qte break return total ``` ### 2.2 - Temps de preparation ```python def temps_preparation(commande, menu, menus_bestof): temps_par_categorie = {} for article, qte in commande["items"]: if article in menus_bestof: # Best-Of = burger + frites + boisson # Ajouter le temps du burger (le plus long) temps_par_categorie["burgers"] = max( temps_par_categorie.get("burgers", 0), 3 ) else: # Trouver la categorie et le temps for categorie, articles in menu.items(): if article in articles: temps = articles[article]["temps_prep"] # Garder le max par categorie (preparation parallele) temps_par_categorie[categorie] = max( temps_par_categorie.get(categorie, 0), temps ) break # Additionner les temps des differentes categories total = 0 for temps in temps_par_categorie.values(): total = total + temps return total ``` ### 2.3 - Calories totales ```python def calories_commande(commande, menu, menus_bestof): total_calories = 0 for article, qte in commande["items"]: if article in menus_bestof: total_calories = total_calories + menus_bestof[article]["calories"] * qte else: for categorie, articles in menu.items(): if article in articles: total_calories = total_calories + articles[article]["calories"] * qte break return total_calories ``` --- ## Exercice 3 (Bonus) ### 3.1 - Ticket de caisse ```python def generer_ticket(commande, menu, menus_bestof): lignes = [] lignes.append("=" * 30) lignes.append("COMMANDE #" + str(commande["id"])) lignes.append("Heure: " + commande["heure"]) lignes.append("-" * 30) total = 0 for article, qte in commande["items"]: # Trouver le prix prix = 0 if article in menus_bestof: prix = menus_bestof[article]["prix"] else: for cat, articles in menu.items(): if article in articles: prix = articles[article]["prix"] break sous_total = prix * qte total = total + sous_total lignes.append(str(qte) + "x " + article + " : " + str(sous_total) + " euros") lignes.append("-" * 30) lignes.append("TOTAL: " + str(total) + " euros") lignes.append("=" * 30) return "\n".join(lignes) ``` ### 3.2 - Repas optimal ```python def repas_optimal(menu, budget, calories_max): meilleur_repas = None meilleur_score = -1 for burger_nom, burger in menu["burgers"].items(): for accomp_nom, accomp in menu["accompagnements"].items(): for boisson_nom, boisson in menu["boissons"].items(): prix = burger["prix"] + accomp["prix"] + boisson["prix"] calories = burger["calories"] + accomp["calories"] + boisson["calories"] if prix <= budget and calories <= calories_max: # Score = satisfaction / prix satisfaction = 100 - calories / 10 score = satisfaction / prix if score > meilleur_score: meilleur_score = score meilleur_repas = { "burger": burger_nom, "accompagnement": accomp_nom, "boisson": boisson_nom, "prix_total": prix, "calories_total": calories } return meilleur_repas ``` ### 3.3 - Analyse de la file ```python def analyser_file(commandes, menu, menus_bestof): chiffre_affaires = 0 # Compter les articles compteur_articles = {} for cmd in commandes: chiffre_affaires = chiffre_affaires + calculer_prix(cmd, menu, menus_bestof) for article, qte in cmd["items"]: if article not in compteur_articles: compteur_articles[article] = 0 compteur_articles[article] = compteur_articles[article] + qte # Trouver l'article le plus commande article_star = None max_qte = 0 for article, qte in compteur_articles.items(): if qte > max_qte: article_star = article max_qte = qte return { "nb_commandes": len(commandes), "chiffre_affaires": chiffre_affaires, "article_plus_commande": article_star } ``` --- ## Pieges a eviter 1. **Best-Of vs articles individuels** : verifiez d'abord si l'article est un Best-Of avant de chercher dans le menu 2. **Temps de preparation** : meme categorie = parallele (max), categories differentes = sequentiel (somme) 3. **Prix avec decimales** : utilisez round() pour arrondir 4. **Quantites** : n'oubliez pas de multiplier par la quantite ! 5. **Articles avec tailles** : "Frites (M)" et "Frites (G)" sont des articles differents