-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_entry.py
47 lines (40 loc) · 1.47 KB
/
data_entry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from datetime import datetime
#specify the date format
date_format = "%d-%m-%Y"
CATEGORY_OPTIONS = {"I": "Income", "E": "Expense"}
#get the date from the user
def get_date(prompt, allow_default=False):
try:
date_str = input(prompt)
if allow_default and not date_str:
return datetime.today().strftime(data_format)
else:
return date_str
except ValueError:
print("Invalid date format. Please use DD-MM-YYYY")
return get_date(prompt, allow_default)
#get the amount from the user
def get_amount():
try:
amount = float(input("Enter the amount: "))
if amount < 0:
print("Amount cannot be negative. Please enter a positive amount.")
return get_amount()
return amount
except ValueError:
print("Invalid amount format. Please use numbers")
return get_amount()
#get the category from the user
def get_category():
category = input("Enter the category ('I' for Income or 'E' for expense): ").upper()
if category not in ['I', 'E']:
print("Invalid category. Please enter 'I' for Income or 'E' for expense.")
return get_category()
if category in CATEGORY_OPTIONS:
return CATEGORY_OPTIONS[category]
else:
print("Invalid category. Please enter 'I' for Income or 'E' for expense.")
return get_category()
#get the description from the user
def get_description():
return input("Enter the description (optional): ")