Menu
CoddyTech
flag Ar iconالعربيةdown icon

تعديل جهة اتصال

جزء من قسم المنطق وتدفّق التنفيذ في رحلة GDScript على Coddy. الدرس 27 من 78.

challenge icon

التحدي

سهل

تعامل مع الاختيار 3، وهو تعديل جهة اتصال. احتفظ بكل ما كتبته في الخطوات السابقة.

اكتب edit_contact(contact_book: Dictionary). تقرأ الدالة اسمًا.

  • إذا لم يكن الاسم موجودًا في contact_book، فاطبع Contact not found! ولا تقرأ أي شيء آخر.
  • وإلا، فاقرأ ثلاثة أسطر: رقم الهاتف الجديد، والبريد الإلكتروني الجديد، والعنوان الجديد، بهذا الترتيب. السطر الفارغ يعني «الاحتفاظ بالقيمة الحالية». حدّث الحقول غير الفارغة، ثم اطبع Contact updated successfully!

على سبيل المثال، إذا كانت Alice موجودة في دفتر جهات الاتصال وكان الإدخال بعد الاختيار 3 كما يلي:

Alice
987-654-3210

456 Elm St

سيصبح رقم هاتف Alice هو 987-654-3210 وعنوانها 456 Elm St، وستحتفظ ببريدها الإلكتروني.

في الحلقة، أضف فرعًا elif للاختيار "3" يستدعي edit_contact(contact_book).

جرّب بنفسك

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

جميع دروس المنطق وتدفّق التنفيذ

تدرّب بنفسك: مترجم GDScript عبر الإنترنت