I have written this program as my final project. How would I write a pytest file to assure the program is properly tested. It really confused me since the functions I implemented in the program do NOT return values when executed. Please give an example of get_user_input() function and how can I pytest assure it.
import sys
def main():
print("~~~~~~~~~~~~~~~~~~~~~~~~~ Welcome to the Expense Tracker ~~~~~~~~~~~~~~~~~~~~~~~~~")
print("This program helps track your total expenses according to their categories")
print("Please Note that the categories are Utilities, Rent, Insurance, Entertainment, Travel & Payroll")
categories = ["utilities", "rent", "insurance", "entertainment", "travel", "payroll"]
expenses = {category: [] for category in categories} #Initilizes a dictionary with empty key values (to be appended later by the function add_expense())
while True:
question = input("Type 1 to add one more or 2 to Quit the program to see the result: ").strip()
if question == "1":
get_user_input(categories, expenses)
elif question == "2":
view_expenses(expenses)
sys.exit("System Quitted") # Exit the program
else:
print("Invalid Input, Please type 1 to Continue or 2 to Quit")
def add_expense(amount, category, expenses):
expenses[category].append(float(amount)) # Append the expense amount to the category list
print("Expense added successfully!")
def view_expenses(expenses):
print("\nCurrent Expenses:")
total_expenses = 0
for category, items in expenses.items():
total_category = sum(items)
total_expenses += total_category
print(f"{category.capitalize()}: ${total_category:.2f}")
print(f"\nTotal Expenses: ${total_expenses:.2f}")
def get_user_input(categories, expenses):
while True:
user_input = input("Please enter the amount in USD (WITHOUT DOLLAR SIGN) and the category of the expense separated by a comma: ").replace(" ", "").lower()
if "," not in user_input:
print("Invalid input format. Please use the format: amount, category")
continue
amount, category = user_input.split(",")
if category not in categories:
print("Invalid Category!")
continue
else:
add_expense(amount, category, expenses)
break
if __name__ == "__main__":
main()
Another off-topic question. Can that project be considered as enough for the final project? or is it not as complicated as it is required to be? What could be some tips/advices for tuning it?
Thanks!!
while True
and stop using that idiom. Everywhere you use it there is no need and there is a proper loop you could be using instead that would demonstrate you've actually learned techniques of python.