Menu
CoddyTech

Kontakt bearbeiten

Teil des Abschnitts Logik & Ablaufsteuerung der GDScript-Journey von Coddy. Lektion 27 von 78.

challenge icon

Aufgabe

Einfach

Behandle die Auswahl 3, um einen Kontakt zu bearbeiten. Behalte alles bei, was du in den vorherigen Schritten geschrieben hast.

Schreibe edit_contact(contact_book: Dictionary). Die Funktion liest einen Namen ein.

  • Wenn der Name nicht in contact_book enthalten ist, gib Contact not found! aus und lies nichts weiter ein.
  • Andernfalls lies drei Zeilen ein: die neue Telefonnummer, die neue E-Mail-Adresse und die neue Adresse, in dieser Reihenfolge. Eine leere Zeile bedeutet "keep the current value". Aktualisiere die nicht leeren Felder und gib dann Contact updated successfully! aus.

Wenn Alice beispielsweise im Adressbuch steht und die Eingabe nach der Auswahl 3 lautet:

Alice
987-654-3210

456 Elm St

Dann erhält Alice die Telefonnummer 987-654-3210 und die Adresse 456 Elm St und behält ihre E-Mail-Adresse.

Füge in der Schleife einen elif-Zweig für "3" hinzu, der edit_contact(contact_book) aufruft.

Probier es selbst

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 == "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!")

Alle Lektionen in Logik & Ablaufsteuerung

Übe selbstständig: Online-GDScript-Compiler