Add Contact
Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 25 of 78.
Challenge
EasyNow 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:
- An empty dictionary
var contact_book := {} - A
while true:loop that callsdisplay_menu()and then reads the choice as text - For the choice
"1", calladd_contact(contact_book). For"6", leave the loop withbreak. 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. ExitTry 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
1Strings In Depth
String MethodsSearching in StringsReplace and TrimSplit and JoinRecap - Username Check4Dictionaries Part 2
Nested DictionariesArrays of DictionariesMerging DictionariesCopying DictionariesRecap - Frequency Counter2Advanced Decision Making
Match With VariablesMatch GuardsArray PatternsDictionary PatternsMembership ChecksRecap - Command Parser5Contact Book Application
Display MenuAdd Contact8Student Records Manager
Project OverviewAdd Student3Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysJagged ArraysCommon 2D Array PatternsRecap - Game Board6Sets with Dictionaries
What is a Set?Dictionary as a SetAdding and RemovingUnion and IntersectionSet DifferenceRecap - Remove Duplicates9Advanced Data Aggregation
Summing ValuesFinding Minimum and MaximumCustom SortingRecap - Dictionary SorterPractice on your own: Online GDScript compiler