Menu
CoddyTech

עריכת איש קשר

חלק מהיחידה לוגיקה וזרימת תוכנית במסלול ה-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 אונליין