-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path19. monthly_expense_month.py
36 lines (30 loc) · 1.31 KB
/
19. monthly_expense_month.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
"""
This module provides functionality to track and identify the month of an expense based on its amount.
"""
expense_list: list[int]= [2340, 2500, 2100, 3100, 2980]
month: list[str]= ["January", "February", "March", "April", "May"]
def expense_tracker(expense_amnt: int)->str:
"""
This function finds the month in which an expense occurred based on the amount.
:param expense_amnt: The expense amount to search for.
:return: The name of the month if found or 'not found' if the amount does not match any recorded expenses.
"""
for i in range(len(expense_list)):
# Check if the current expense matches the input amount
if expense_list[i]== expense_amnt:
return month[i]
return "not found"
# Using try-except to handle potential ValueError from user input
try:
# Enter the expense amount
expense: int= int(input("Enter the expense amount: "))
# Finding the month in which the expense occured using expense_tracker function
expense_month: str= expense_tracker(expense)
# Printing the result if month not found
if expense_month== "not found":
print(f"Failed to find the expense month.")
# Printing the result if month is found
else:
print(f"The expense was {expense} on {expense_month}")
except ValueError as e:
print(f"Error: {e}")