Menu
CoddyTech

Tudo junto

Parte da seção Lógica e fluxo do Journey de GDScript da Coddy. Lição 30 de 78.

challenge icon

Desafio

Fácil

A última etapa organiza o loop e trata as escolhas inválidas. Mantenha tudo o que você escreveu nas etapas anteriores.

  1. Substitua a cadeia de if / elif no loop por uma instrução match choice:, com um padrão para cada escolha de "1" a "6". break dentro de um padrão ainda sai do loop while.
  2. Adicione o padrão curinga _: para qualquer outra escolha. Ele imprime:
Invalid choice. Please try again.

Depois da mensagem, o loop exibe o menu e lê a próxima escolha, como antes.

Experimente você mesmo

extends Node

func _ready():
	var contact_book := {}

	while true:
		display_menu()
		var choice := OS.read_string_from_stdin()
		if choice == "1":
			add_contact(contact_book)
		elif choice == "2":
			view_contact(contact_book)
		elif choice == "3":
			edit_contact(contact_book)
		elif choice == "4":
			delete_contact(contact_book)
		elif choice == "5":
			list_all_contacts(contact_book)
		elif choice == "6":
			break

func display_menu() -> void:
	print("Contact Book Menu:")
	print("1. Add Contact")
	print("2. View Contact")
	print("3. Edit Contact")
	print("4. Delete Contact")
	print("5. List All Contacts")
	print("6. Exit")

func add_contact(contact_book: Dictionary) -> void:
	var contact_name := OS.read_string_from_stdin()
	if contact_book.has(contact_name):
		print("Contact already exists!")
		return
	var phone := OS.read_string_from_stdin()
	var email := OS.read_string_from_stdin()
	var address := OS.read_string_from_stdin()
	contact_book[contact_name] = {"phone": phone, "email": email, "address": address}
	print("Contact added successfully!")

func view_contact(contact_book: Dictionary) -> void:
	var contact_name := OS.read_string_from_stdin()
	if contact_book.has(contact_name):
		var contact: Dictionary = contact_book[contact_name]
		print("Name: ", contact_name)
		print("Phone: ", contact["phone"])
		print("Email: ", contact["email"])
		print("Address: ", contact["address"])
	else:
		print("Contact not found!")

func edit_contact(contact_book: Dictionary) -> void:
	var contact_name := OS.read_string_from_stdin()
	if contact_book.has(contact_name):
		var contact: Dictionary = contact_book[contact_name]
		var phone := OS.read_string_from_stdin()
		var email := OS.read_string_from_stdin()
		var address := OS.read_string_from_stdin()
		if phone != "":
			contact["phone"] = phone
		if email != "":
			contact["email"] = email
		if address != "":
			contact["address"] = address
		print("Contact updated successfully!")
	else:
		print("Contact not found!")

func delete_contact(contact_book: Dictionary) -> void:
	var contact_name := OS.read_string_from_stdin()
	if contact_book.has(contact_name):
		contact_book.erase(contact_name)
		print("Contact deleted successfully!")
	else:
		print("Contact not found!")

func list_all_contacts(contact_book: Dictionary) -> void:
	if contact_book.is_empty():
		print("No contacts available.")
	else:
		for contact_name in contact_book:
			var contact: Dictionary = contact_book[contact_name]
			print("Name: ", contact_name)
			print("Phone: ", contact["phone"])
			print("Email: ", contact["email"])
			print("Address: ", contact["address"])
			print()

Todas as lições de Lógica e fluxo

Pratique por conta própria: Compilador de GDScript online