Menu
CoddyTech

Consulter un contact

Fait partie de la section Logique et flux d’exécution du Journey GDScript de Coddy. Leçon 26 sur 78.

challenge icon

Défi

Facile

Gérez le choix 2, qui consiste à afficher un contact. Conservez tout ce que vous avez écrit aux étapes précédentes.

Écrivez view_contact(contact_book: Dictionary). Cette fonction lit un nom. Si le nom se trouve dans contact_book, elle affiche les quatre détails de ce contact dans ce format :

Name: Alice
Phone: 123-456-7890
Email: alice@example.com
Address: 123 Main St

Sinon, elle affiche :

Contact not found!

Dans la boucle, ajoutez une branche elif pour "2" qui appelle view_contact(contact_book), avant la branche pour "6". La fonction n’affiche rien d’autre : aucun texte d’invite.

Essayez vous-même

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

Toutes les leçons de Logique et flux d’exécution

Entraînez-vous par vous-même : Compilateur GDScript en ligne