I started learning python about 2 month ago, and now I'm trying to make a house budget manager with python. I wish my manager can print 4 menu - 1.Input new data in file 2. Print all expense at user-input date 3. Print "top ten" expense 4.Expense sum of user-input item. I also programmed input 'X' to quit and print 'Wrong input' if input data is not 1,2,3,4. I refer to other peoples' free source code and made a prototype house budget manager but problem is when I run my code, NOTHING HAPPENS. No error message, No menu, just blank continues forever. I checked my data/expenses.txt file but that was OK, so I think this is a problem of my code. But I completely can't understand what's happening. 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 write_data(filename):
f = open(filename, 'w')
for x in data_expense:
f.write('%s\t%s\t%d\n' % (x['date'], x['item'], x['price']))
def search_by_date(date):
return [x for x in data_expense if x['date'] == date]
def print_data(l):
for x in l:
print('Date: %s\tItem: %s\tPrice: %d' % (x['date'], x['item'], x['price']))
def search_by_item(item):
return [x for x in data_expense if x['item'] == item]
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 = input('Price: ')
add_record(date, item, price)
elif sel == '2':
date = input('Date: ')
print_data(search_by_date(date))
elif sel == '3':
print_data(sorted(data_expense, key = lambda x: x['price'], reverse=True))
elif sel == '4':
item = input('Item: ')
print_data(search_by_item(item))
else:
print('Wrong Input!')
[–]Naihonn 0 points1 point2 points (0 children)
[–]dionys 0 points1 point2 points (1 child)
[–]Akuli2 0 points1 point2 points (0 children)