-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_entry.py
40 lines (30 loc) · 1.16 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
from datetime import datetime
date_format = "%d-%m-%Y"
CATEGORIES = {"I": "Income", "E": "Expense"}
def get_date(prompt, allow_default=False):
date_str = input(prompt)
if allow_default and not date_str:
return datetime.today().strftime(date_format)
try:
valid_date = datetime.strptime(date_str, date_format)
return valid_date.strftime(date_format)
except ValueError:
print("Invalid date format. Please enter the date in dd-mm-yyyy format!")
return get_date(prompt, allow_default)
def get_amount():
try:
amount = float(input("Enter the amount: "))
if amount <= 0:
raise ValueError("Amount must be a non-zero non-negative value!")
return amount
except ValueError as e:
print(e)
return get_amount()
def get_category():
category = input("Enter the category ('I' for Income or 'E' for Expense): ").upper()
if category in CATEGORIES:
return CATEGORIES[category]
print("Invalid category. Please enter 'I' for Income or 'E' for Expense!")
return get_category()
def get_description():
return input("Enter a description (optional): ")