from datetime import datetime def read_data(filename): records = [] with open ('data.txt','r') as readfile: for k in readfile: k = k.strip() list = k.split(',') date = datetime.strptime(list[0], '%d-%m-%Y') date1 = datetime.strftime(date, '%d/%m/%Y') branch = list[1] daily_sale = float(list[2]) transaction = int(list[3]) rec = [date1,branch,daily_sale,transaction] records.append(rec) return records def print_all_records(records): template = '{0:<20} {1:<15} {2:<15} {3:<5}' print('*' * 10 + ' Printing all records ' + '*' * 10) print(template.format('Date','Branch','Daily sale','Transaction')) print('-' * 64) template = '{0:<20} {1:<15} ${2:<15} {3:<5}' for k in records: print(template.format(k[0],k[1],k[2],k[3])) def query_branch_sale(records): print('*' * 10 + ' Querying total sales by branch ' + '*' * 10) branch_name = input('Please enter the branch name ').title() avarage_sale = 0 total_transaction = 0 total_sales = 0 avarage_transaction = 0 count = 0 for k in records: if branch_name == k[1]: total_sales += k[2] total_transaction += k[3] count += 1 if count == 0: avarage_sale = 0 avarage_transaction = 0 else: avarage_sale = total_sales/count avarage_transaction = total_transaction/count print('Total sale by ' + branch_name + ': ' + '$' + str(total_sales)) print('Average sale by ' + branch_name + ' $' + str(avarage_sale)) print('Total transaction by ' + branch_name + ' ' + str(total_transaction)) print('Average number of transaction per day by ' + branch_name + ' ' + str(avarage_transaction)) def query_record_by_date(records): print('*' * 10 + ' Querying a records by date ' + '*' * 10) date = input('Please enter a date (dd mm yyyy) ') user_date = datetime.strptime(date, '%d %m %Y') output_date = datetime.strftime(user_date, '%d/%m/%Y') count = 0 template = '{0:<20} {1:<15} ${2:<15} {3:<5}' for k in records: if output_date == k[0]: print(template.format(k[0],k[1],k[2],k[3])) ATV = round(k[2]/k[3],2) print('ATV for the day: ' + str(ATV)) break else: count += 1 if count == len(records): print('No record found for ' + date) def query_period_sale(records): print('*' * 10 + ' Querying sales between two date ' + '*' * 10) date1 = input('Please enter the start date(dd mm yyyy): ') date2 = input('Please enter the end date(dd mm yyyy): ') user_date0 = datetime.strptime(date1, '%d %m %Y') user_date1 = datetime.strptime(date2, '%d %m %Y') output_date0 = datetime.strftime(user_date0, '%d/%m/%Y') output_date1 = datetime.strftime(user_date1, '%d/%m/%Y') total = 0 for k in records: r_date = datetime.strptime(k[0], '%d/%m/%Y') if output_date0 <= r_date and output_date1 >= r_date: total += round(k[2],2) print('Total sale between ' + date1 + ' and ' + date2 + ' is $' + str(total)) def main(): records = read_data('data.txt') print_all_records(records) query_branch_sale(records) query_record_by_date(records) query_period_sale(records) main()
このようなエラーが出ました。Googleで検索しても解決法が見つからなかったので、どなたかコード見ていただけないでしょうか。
よろしくおねがいします。
if output_date0 <= r_date and output_date1 >= r_date:
TypeError: '<=' not supported between instances of 'str' and 'datetime.datetime'
回答2件
あなたの回答
tips
プレビュー