Menu
CoddyTech

Edit Kontak

Termasuk bagian Logika dan Alur dalam perjalanan GDScript di Coddy. Pelajaran 27 dari 78.

challenge icon

Tantangan

Mudah

Tangani pilihan 3, yaitu mengedit kontak. Pertahankan semua yang kamu tulis pada langkah-langkah sebelumnya.

Tulis edit_contact(contact_book: Dictionary). Fungsi ini membaca sebuah nama.

  • Jika nama tersebut tidak ada di contact_book, cetak Contact not found! dan jangan membaca apa pun lagi.
  • Jika tidak, baca tiga baris: nomor telepon baru, email baru, dan alamat baru, dalam urutan tersebut. Baris kosong berarti "pertahankan nilai saat ini". Perbarui field yang tidak kosong, lalu cetak Contact updated successfully!

Contohnya, jika Alice ada di buku kontak dan input setelah pilihan 3 adalah:

Alice
987-654-3210

456 Elm St

Nomor telepon Alice menjadi 987-654-3210 dan alamatnya menjadi 456 Elm St, sedangkan emailnya tetap.

Di dalam perulangan, tambahkan cabang elif untuk "3" yang memanggil edit_contact(contact_book).

Coba sendiri

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

Semua pelajaran di Logika dan Alur

Latihan sendiri: Compiler GDScript online