Menu
CoddyTech

Add Contact

Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 25 of 78.

challenge icon

Challenge

Easy

Now the program keeps the contacts and reads the choices of the user. Keep everything you wrote in the previous steps.

1. The contact book. Each contact is stored under its name, and its value is a dictionary with three keys:

{"Alice": {"phone": "123-456-7890", "email": "alice@example.com", "address": "123 Main St"}}

2. The function. Write add_contact(contact_book: Dictionary) below the other function. It reads a name. If the name is already in contact_book, it prints Contact already exists! and stops without reading anything else. Otherwise it reads three more lines (phone, email, address), stores the contact and prints:

Contact added successfully!

3. The loop. In _ready(), replace the single display_menu() call with:

  1. An empty dictionary var contact_book := {}
  2. A while true: loop that calls display_menu() and then reads the choice as text
  3. For the choice "1", call add_contact(contact_book). For "6", leave the loop with break. Ignore every other choice for now.

The menu is printed before every choice. For the input 1, Alice, 123-456-7890, alice@example.com, 123 Main St, 6, the output is:

Contact Book Menu:
1. Add Contact
2. View Contact
3. Edit Contact
4. Delete Contact
5. List All Contacts
6. Exit
Contact added successfully!
Contact Book Menu:
1. Add Contact
2. View Contact
3. Edit Contact
4. Delete Contact
5. List All Contacts
6. Exit

Try it yourself

extends Node

func _ready():
	display_menu()

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

All lessons in Logic & Flow

Practice on your own: Online GDScript compiler