From cbdf3cbad7074ead4e77fbbf33ea16bbe526e93c Mon Sep 17 00:00:00 2001 From: Trygve Date: Fri, 10 Nov 2023 16:18:22 +0100 Subject: [PATCH] Oppgave 6 --- shop_sum.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 shop_sum.py diff --git a/shop_sum.py b/shop_sum.py new file mode 100644 index 0000000..50d2f9a --- /dev/null +++ b/shop_sum.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +"""Created on 6.11""" +__author__ = "Trygve B. Nomeland" +__email__ = "trygve.borte.nomeland@nmbu.no" + +def read_shopping_list(file_path: str): + with open(file_path, 'r') as f: + data = [i.strip('\n').split(' ') for i in f.readlines()[2:]] + item = [i[0] for i in data] + price = [float(i[1])*float(i[2]) for i in data] + return {k:v for (k,v) in zip(item, price)} + +def main(): + shopping_dict = {key: val for key, val in sorted(read_shopping_list('julehandel.txt').items(), key = lambda ele: ele[0])} + print('-'*40) + for item, price in shopping_dict.items(): + print(f'{item:<30} {price:>10.2f} kr') + print('-'*40) + print(f'{"Sum":<30} {sum(shopping_dict.values()):>10.2f} kr') + +if __name__ == '__main__': + main() \ No newline at end of file