Respuesta :

Answer:

import os

def load_file(filename):

   if os.path.isfile(filename) is not True:return None

   sales_dict={}

   with open(filename,'r') as infile:

       for line in infile.readlines():

           line = line.strip().split(',')

           try:

               sales_dict[line[0].lower()] = float(line[1].strip())

           except:

               continue

   return sales_dict

def printMonthlySales(sales_dict):

   month_names=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']

   for month in month_names:

       print('{0} - {1:>8}'.format(month,sales_dict.get(month.lower())))

def printYearlySummary(sales_dict):

   total_sales=0

   for sales in sales_dict.values():

       total_sales+=sales

   print('Yearly Total: ${0}'.format(total_sales))

def edit(sales_dict,filename):

   month_names=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']

   month_name=input('Enter month for which you want to edit the sales amount: ').lower()

   if month_name in month_names:

       sales_amount=float(input('Enter sales amount: '))

       for month,sales in sales_dict.items():

           if month==month_name:

               sales_dict[month_name]=sales_amount

               update_file(sales_dict,filename)

   else:

       print('Invalid month name entered.')

# function to update the file

def update_file(sales_dict, filename):

   with open(filename,'w') as outfile:

       outfile.write('Month,Sales\n')

       month_names=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']

       for month in month_names:

           outfile.write('{0},{1}\n'.format(month.title(),sales_dict.get(month)))

       print('{} saved successfully'.format(filename))

def main():

   filename='sales.csv' # file name that needs to be read and updated

   sales_dict = load_file(filename)

   if sales_dict is None:

       print('Unable to read data from file: {}'.format(filename))

       return

   print('COMMAND MENU')

   while True:

       print('[1] View Monthly Sales')

       print('[2] View Yearly Summary')

       print('[3] Edit Sales')

       print('[4] Quit')

       choice=input('Enter your choice: ')

       if choice =='1':

           printMonthlySales(sales_dict)

       elif choice=='2':

           printYearlySummary(sales_dict)

       elif choice=='3':

           edit(sales_dict,filename)

       elif choice=='4':break

       else:print('Invalid selection')

main()

Explanation:

ACCESS MORE