عرض الكل
جزء من قسم المنطق وتدفّق التنفيذ في رحلة GDScript على Coddy. الدرس 29 من 78.
التحدي
سهلتعامل مع الخيار 5، واعرض جميع جهات الاتصال. احتفظ بكل ما كتبته في الخطوات السابقة.
اكتب list_all_contacts(contact_book: Dictionary). لا تقرأ أي إدخال. إذا كان contact_book فارغًا، اطبع:
No contacts available.وإلا، فاطبع كل جهة اتصال حسب ترتيب إضافتها، بالتنسيق نفسه المكوّن من أربعة أسطر كما في view_contact()، متبوعًا بسطر فارغ. عند وجود Alice وBob في دفتر جهات الاتصال، يكون الناتج:
Name: Alice
Phone: 123-456-7890
Email: alice@example.com
Address: 123 Main St
Name: Bob
Phone: 234-567-8901
Email: bob@example.com
Address: 456 Oak Aveفي الحلقة، أضف فرع elif للقيمة "5" يستدعي list_all_contacts(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 == "3":
edit_contact(contact_book)
elif choice == "4":
delete_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!")
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!")
جميع دروس المنطق وتدفّق التنفيذ
1السلاسل النصية بالتفصيل
أساليب السلاسل النصيةالبحث داخل السلاسل النصيةالاستبدال وإزالة المسافات الطرفيةالتقسيم والدمجمراجعة - التحقق من اسم المستخدم4القواميس، الجزء الثاني
القواميس المتداخلةمصفوفات من القواميسدمج القواميسنسخ القواميسمراجعة - عدّ التكرارات2اتخاذ القرارات المتقدم
المطابقة باستخدام المتغيراتشروط المطابقةأنماط المصفوفاتأنماط القواميسالتحقق من العضويةمراجعة - محلل الأوامر5تطبيق دفتر جهات الاتصال
عرض القائمةإضافة جهة اتصال8مدير سجلات الطلاب
نظرة عامة على المشروعإضافة طالب3المصفوفات متعددة الأبعاد
أساسيات المصفوفات ثنائية الأبعادالوصول إلى عناصر المصفوفة ثنائية الأبعادالحلقات المتداخلة مع المصفوفات ثنائية الأبعادمراجعة - المصفوفات ثنائية الأبعادالمصفوفات المتعرجةأنماط شائعة للمصفوفات ثنائية الأبعادمراجعة - لوحة اللعبة6المجموعات باستخدام القواميس
ما هي المجموعة؟القاموس كمجموعةالإضافة والإزالةالاتحاد والتقاطعفرق المجموعاتمراجعة - إزالة العناصر المكررة9تجميع البيانات المتقدم
جمع القيمإيجاد الحد الأدنى والحد الأقصىالترتيب المخصصمراجعة - أداة ترتيب القواميستدرّب بنفسك: مترجم GDScript عبر الإنترنت