Oppgave 6

This commit is contained in:
Trygve 2023-11-10 16:18:22 +01:00
parent 683535b5af
commit cbdf3cbad7

22
shop_sum.py Normal file
View File

@ -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()