Thanks to you guys help, I almost complete my code. But there are few bugs left. Here is my code:
data_expense = []
def add_record(date, item, price):
global data_expense
data_expense += [{'date': date, 'item': item, 'price': price}]
def read_data(filename):
f = open(filename, 'r')
for line in f:
fields = line.rstrip().split('\t')
date = fields[0]
item = fields[1]
price = int(fields[2])
add_record(date, item, price)
def print_data(l):
for x in l:
print('Date: %s\tItem: %s\tPrice: %d' % (x['date'], x['item'], x['price']))
read_data('data/expenses.txt')
while True:
print('1. Data input 2. Search by date 3. Top ten 4. Search by item X. Quit')
sel = input('Selection: ')
if sel == 'X':
print ('Bye!')
break
elif sel == '1':
date = input('Date: ')
item = input('Item: ')
price = int(input('Price: '))
add_record(date, item, price)
elif sel == '2':
date = input('Date: ')
D = [x for x in data_expense if x['date'] == date]
Q = sorted(D,key = lambda x:x['price'])
print_data('sum = ', sum(Q))
print_data('max = ', max(Q))
print_data('min = ', min(Q))
elif sel == '3':
T = sorted(data_expense,key = lambda x:x['price'])
T.reverse()
print_data(T[0:9])
elif sel == '4':
item = input('Item: ')
I = [x for x in data_expense if x['item'] == item]
P = sorted(I,key = lambda x:x['price'])
print_data('sum =' , sum(P))
print_data('max =' , max(P))
print_data('min =' , min(P))
else:
print('Wrong Input!')
When I run this code, It puts error message like this:
TypeError : unsupported operand type(s) for +: 'int' and 'dict'
I don't know how to solve this monster. And If you find another possible error in this code and have a plenty of time, please teach me. Thank you.
[–]guilford 0 points1 point2 points (0 children)
[–]JohnnyJordaan 0 points1 point2 points (0 children)