Menu
CoddyTech

Очистка всех данных

Часть раздела Основы путешествия по GDScript на Coddy. Урок 88 из 92.

challenge icon

Задание

Легко

Обработай выбор 4, удалив все расходы. Сохрани всё, что ты написал на предыдущих шагах.

Удали все расходы из массива, затем выведи:

All expenses cleared.

После этого при выборе 2 и 3 выводится No expenses recorded yet., пока не будет добавлен новый расход.

Попробуйте сами

extends Node

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

	# Показать меню один раз
	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")

	# Пустой массив для расходов
	var expenses: Array[float] = []

	while true:
		# Получить выбор пользователя
		var choice := OS.read_string_from_stdin()

		if choice == "1":
			# Добавить новый расход
			var amount := float(OS.read_string_from_stdin())
			expenses.append(amount)
			print("Expense added successfully!")
		elif choice == "2":
			# Показать все расходы
			if expenses.is_empty():
				print("No expenses recorded yet.")
			else:
				print("Your expenses:")
				for i in range(expenses.size()):
					print(i + 1, ". ", expenses[i])
		elif choice == "3":
			# Вычислить общую сумму и среднее значение
			if expenses.is_empty():
				print("No expenses recorded yet.")
			else:
				var total := 0.0
				for expense in expenses:
					total += expense
				var average := total / expenses.size()
				print("Total expense: ", total)
				print("Average expense: ", average)
		elif choice == "5":
			# Выйти из программы
			print("Exiting the Daily Expense Tracker. Goodbye!")
			break

Все уроки раздела Основы

Потренируйтесь самостоятельно: Онлайн-компилятор GDScript