====== LU02.L02 - Refactoring unpure -> pure ====== # Unpure function BASKET = [{'Produkt': 'T-Shirt', 'Preis': 20}, {'Produkt': 'Hose', 'Preis': 50}] DISCOUNT = 0.1 # Unpure function def calculate_total_unpure(): global BASKET, DISCOUNT total = sum(item['Preis'] for item in BASKET) discount = total * DISCOUNT total = total - discount print("Gesamtpreis:", total) return total # Pure function def calculate_total_pure(basket, discount): total = sum(item['Preis'] for item in basket) discount_amount = total * discount total = total - discount_amount return total if __name__ == "__main__": print("Unpure function:") calculate_total_unpure() print("Pure function:") total = calculate_total_pure(BASKET, DISCOUNT) print("Gesamtpreis:", total) ---- [[https://creativecommons.org/licenses/by-nc-sa/4.0/ch/|{{https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png}}]] (c) Kevin Maurizi