TP02
This commit is contained in:
63
TP02.py
Normal file
63
TP02.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
|
||||
|
||||
THRESHOLD = Decimal('200.00')
|
||||
|
||||
def read_amount(prompt: str) -> Decimal | None:
|
||||
"""Demande un montant à l'utilisateur, retourne None si 0 (arrêt)."""
|
||||
while True:
|
||||
s = input(prompt).strip().replace(',', '.')
|
||||
if s == '':
|
||||
print("Entrée vide — réessaie.")
|
||||
continue
|
||||
try:
|
||||
d = Decimal(s)
|
||||
except InvalidOperation:
|
||||
print("Montant invalide — entre un nombre.")
|
||||
continue
|
||||
if d < 0:
|
||||
print("Le montant ne peut pas être négatif.")
|
||||
continue
|
||||
if d == 0:
|
||||
return None
|
||||
return d.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
|
||||
|
||||
def afficher_stats(montants: list[Decimal]) -> None:
|
||||
if not montants:
|
||||
print("\nAucun chèque saisi pour le moment.\n")
|
||||
return
|
||||
|
||||
count = len(montants)
|
||||
total = sum(montants)
|
||||
moyenne = (total / count).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
|
||||
|
||||
bas = [m for m in montants if m < THRESHOLD]
|
||||
haut = [m for m in montants if m >= THRESHOLD]
|
||||
|
||||
bas_count, bas_total = len(bas), sum(bas) if bas else Decimal('0.00')
|
||||
haut_count, haut_total = len(haut), sum(haut) if haut else Decimal('0.00')
|
||||
|
||||
min_montant = min(montants)
|
||||
max_montant = max(montants)
|
||||
|
||||
print("\n--- Récapitulatif ---")
|
||||
print(f"Nombre de chèques : {count}")
|
||||
print(f"Montant total : {total} €")
|
||||
print(f"Moyenne des montants : {moyenne} €")
|
||||
print(f"Chèques < {THRESHOLD} € : {bas_count} (total = {bas_total} €)")
|
||||
print(f"Chèques >= {THRESHOLD} € : {haut_count} (total = {haut_total} €)")
|
||||
print(f"Chèque le plus petit : {min_montant} €")
|
||||
print(f"Chèque le plus grand : {max_montant} €\n")
|
||||
|
||||
def main():
|
||||
montants: list[Decimal] = []
|
||||
|
||||
print("Saisie des chèques — entre 0 pour terminer.")
|
||||
while True:
|
||||
montant = read_amount("Montant du chèque en € (0 pour terminer) : ")
|
||||
if montant is None:
|
||||
break
|
||||
montants.append(montant)
|
||||
afficher_stats(montants)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user