Menu
CoddyTech

Visualizzare tutte le spese

Fa parte della sezione Fondamenti del percorso GDScript di Coddy. Lezione 86 di 92.

challenge icon

Sfida

Facile

Gestisci la scelta 2, visualizzando tutte le spese. Mantieni tutto ciò che hai scritto nei passaggi precedenti.

Se l'array delle spese è vuoto, stampa:

No expenses recorded yet.

Altrimenti, stampa Your expenses: e poi ogni spesa con il suo numero, iniziando da 1. Per le spese 23.1, 35.5, 99.99 e 15.2, aggiunte in quest'ordine, l'output è:

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

Una spesa inserita come numero intero viene memorizzata come float, quindi 12 viene mostrato come 12.0.

Provalo tu

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

Tutte le lezioni di Fondamenti

Esercitati da solo: Compilatore GDScript online