Menu
CoddyTech

View All Expenses

Part of the Fundamentals section of Coddy's GDScript journey. Lesson 86 of 92.

challenge icon

Challenge

Easy

Handle the choice 2, viewing all the expenses. Keep everything you wrote in the previous steps.

If the array of expenses is empty, print:

No expenses recorded yet.

Otherwise, print Your expenses: and then each expense with its number, starting at 1. For the expenses 23.1, 35.5, 99.99 and 15.2, added in that order, the output is:

Your expenses:
1. 23.1
2. 35.5
3. 99.99
4. 15.2

An expense entered as a whole number is stored as a float, so 12 is shown as 12.0.

Try it yourself

extends Node

func _ready():
	print("Welcome to the Daily Expense Tracker!")

	# Display the menu once
	print("\nMenu:")
	print("1. Add a new expense")
	print("2. View all expenses")
	print("3. Calculate total and average expense")
	print("4. Clear all expenses")
	print("5. Exit")

	# An empty array for the expenses
	var expenses: Array[float] = []

	while true:
		# Get the choice of the user
		var choice := OS.read_string_from_stdin()

		if choice == "1":
			# Add a new expense
			var amount := float(OS.read_string_from_stdin())
			expenses.append(amount)
			print("Expense added successfully!")
		elif choice == "5":
			# Exit the program
			print("Exiting the Daily Expense Tracker. Goodbye!")
			break

All lessons in Fundamentals

Practice on your own: Online GDScript compiler