[{"data":1,"prerenderedAt":2414},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-contact-book-example":3},{"id":4,"title":5,"body":6,"description":2407,"extension":2408,"meta":2409,"navigation":283,"path":2410,"seo":2411,"stem":2412,"__hash__":2413},"content\u002Fexamples\u002Fpython-contact-book-example.md","Python Contact Book Example",{"type":7,"value":8,"toc":2374},"minimark",[9,13,17,20,39,42,47,50,64,68,71,108,112,119,135,138,218,221,228,232,235,262,265,926,931,968,972,975,1047,1050,1069,1072,1074,1101,1104,1130,1133,1137,1140,1218,1221,1235,1250,1253,1261,1265,1272,1275,1360,1366,1369,1377,1388,1392,1395,1398,1484,1486,1504,1507,1511,1514,1531,1534,1606,1613,1617,1620,1623,1715,1718,1738,1742,1745,1751,1756,1759,1797,1800,1851,1858,1861,1863,1920,1926,1929,1964,1967,1986,1993,1996,1999,2027,2030,2032,2059,2066,2070,2073,2075,2140,2143,2170,2173,2182,2185,2188,2228,2231,2270,2277,2281,2285,2288,2292,2295,2299,2302,2306,2317,2321,2324,2328,2367,2370],[10,11,5],"h1",{"id":12},"python-contact-book-example",[14,15,16],"p",{},"A small contact book project is a great way to practice Python basics.",[14,18,19],{},"In this example, you will build a simple command-line program that:",[21,22,23,27,30,33,36],"ul",{},[24,25,26],"li",{},"stores contact names and phone numbers",[24,28,29],{},"asks the user for input",[24,31,32],{},"searches for contacts",[24,34,35],{},"deletes contacts",[24,37,38],{},"repeats with a menu until the user exits",[14,40,41],{},"This project is beginner-friendly because it uses a few core ideas in a practical way.",[43,44,46],"h2",{"id":45},"what-this-project-builds","What this project builds",[14,48,49],{},"This example creates:",[21,51,52,55,58,61],{},[24,53,54],{},"A simple command-line contact book",[24,56,57],{},"A program that stores contact names and phone numbers",[24,59,60],{},"A menu that lets the user add, view, search, and delete contacts",[24,62,63],{},"A good beginner project for practicing dictionaries and loops",[43,65,67],{"id":66},"skills-this-example-practices","Skills this example practices",[14,69,70],{},"While building this project, you will practice:",[21,72,73,76,84,95,105],{},[24,74,75],{},"Using a dictionary to store related data",[24,77,78,79,83],{},"Using a ",[80,81,82],"code",{},"while"," loop for a repeating menu",[24,85,86,87],{},"Using ",[88,89,91,94],"a",{"href":90},"\u002Fhow-to\u002Fhow-to-get-user-input-in-python\u002F",[80,92,93],{},"input()"," to get user input",[24,96,86,97,100,101,104],{},[80,98,99],{},"if"," and ",[80,102,103],{},"elif"," to handle menu options",[24,106,107],{},"Checking if a key exists before reading or deleting it",[43,109,111],{"id":110},"how-the-contact-data-is-stored","How the contact data is stored",[14,113,114,115,118],{},"The program uses a dictionary named ",[80,116,117],{},"contacts",".",[21,120,121,129],{},[24,122,123,124,128],{},"Each ",[125,126,127],"strong",{},"key"," is a contact name",[24,130,123,131,134],{},[125,132,133],{},"value"," is that contact's phone number",[14,136,137],{},"Example:",[139,140,145],"pre",{"className":141,"code":142,"language":143,"meta":144,"style":144},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","contacts = {\n    \"Alice\": \"1234567890\",\n    \"Bob\": \"5551234567\"\n}\n","python","",[80,146,147,164,192,212],{"__ignoreMap":144},[148,149,152,156,160],"span",{"class":150,"line":151},"line",1,[148,153,155],{"class":154},"su5hD","contacts ",[148,157,159],{"class":158},"smGrS","=",[148,161,163],{"class":162},"sP7_E"," {\n",[148,165,167,171,175,178,181,184,187,189],{"class":150,"line":166},2,[148,168,170],{"class":169},"sjJ54","    \"",[148,172,174],{"class":173},"s_sjI","Alice",[148,176,177],{"class":169},"\"",[148,179,180],{"class":162},":",[148,182,183],{"class":169}," \"",[148,185,186],{"class":173},"1234567890",[148,188,177],{"class":169},[148,190,191],{"class":162},",\n",[148,193,195,197,200,202,204,206,209],{"class":150,"line":194},3,[148,196,170],{"class":169},[148,198,199],{"class":173},"Bob",[148,201,177],{"class":169},[148,203,180],{"class":162},[148,205,183],{"class":169},[148,207,208],{"class":173},"5551234567",[148,210,211],{"class":169},"\"\n",[148,213,215],{"class":150,"line":214},4,[148,216,217],{"class":162},"}\n",[14,219,220],{},"This keeps the project simple and easy to understand.",[14,222,223,224,118],{},"If you are new to dictionaries, see ",[88,225,227],{"href":226},"\u002Flearn\u002Fpython-dictionaries-explained\u002F","Python dictionaries explained",[43,229,231],{"id":230},"build-the-menu-step-by-step","Build the menu step by step",[14,233,234],{},"The basic idea is:",[236,237,238,241,248,253,256],"ol",{},[24,239,240],{},"Start with an empty dictionary",[24,242,243,244,247],{},"Use ",[80,245,246],{},"while True"," to keep showing the menu",[24,249,250,251],{},"Read the user's choice with ",[80,252,93],{},[24,254,255],{},"Run different code based on the choice",[24,257,243,258,261],{},[80,259,260],{},"break"," to exit",[14,263,264],{},"Here is the full working script:",[139,266,268],{"className":141,"code":267,"language":143,"meta":144,"style":144},"contacts = {}\n\nwhile True:\n    print(\"\\n1. Add contact\")\n    print(\"2. View contacts\")\n    print(\"3. Search contact\")\n    print(\"4. Delete contact\")\n    print(\"5. Exit\")\n\n    choice = input(\"Choose an option: \")\n\n    if choice == \"1\":\n        name = input(\"Name: \")\n        phone = input(\"Phone: \")\n        contacts[name] = phone\n        print(\"Contact saved.\")\n\n    elif choice == \"2\":\n        if not contacts:\n            print(\"No contacts found.\")\n        else:\n            for name, phone in contacts.items():\n                print(name, \"-\", phone)\n\n    elif choice == \"3\":\n        name = input(\"Enter name to search: \")\n        if name in contacts:\n            print(name, \"-\", contacts[name])\n        else:\n            print(\"Contact not found.\")\n\n    elif choice == \"4\":\n        name = input(\"Enter name to delete: \")\n        if name in contacts:\n            del contacts[name]\n            print(\"Contact deleted.\")\n        else:\n            print(\"Contact not found.\")\n\n    elif choice == \"5\":\n        print(\"Goodbye!\")\n        break\n\n    else:\n        print(\"Invalid option.\")\n",[80,269,270,279,285,297,320,336,352,368,384,389,411,416,437,458,479,500,517,522,541,555,572,580,609,635,640,658,678,692,720,727,743,748,766,786,799,814,830,837,852,857,875,891,897,902,910],{"__ignoreMap":144},[148,271,272,274,276],{"class":150,"line":151},[148,273,155],{"class":154},[148,275,159],{"class":158},[148,277,278],{"class":162}," {}\n",[148,280,281],{"class":150,"line":166},[148,282,284],{"emptyLinePlaceholder":283},true,"\n",[148,286,287,290,294],{"class":150,"line":194},[148,288,82],{"class":289},"sVHd0",[148,291,293],{"class":292},"s39Yj"," True",[148,295,296],{"class":162},":\n",[148,298,299,303,306,308,312,315,317],{"class":150,"line":214},[148,300,302],{"class":301},"sptTA","    print",[148,304,305],{"class":162},"(",[148,307,177],{"class":169},[148,309,311],{"class":310},"s_hVV","\\n",[148,313,314],{"class":173},"1. Add contact",[148,316,177],{"class":169},[148,318,319],{"class":162},")\n",[148,321,323,325,327,329,332,334],{"class":150,"line":322},5,[148,324,302],{"class":301},[148,326,305],{"class":162},[148,328,177],{"class":169},[148,330,331],{"class":173},"2. View contacts",[148,333,177],{"class":169},[148,335,319],{"class":162},[148,337,339,341,343,345,348,350],{"class":150,"line":338},6,[148,340,302],{"class":301},[148,342,305],{"class":162},[148,344,177],{"class":169},[148,346,347],{"class":173},"3. Search contact",[148,349,177],{"class":169},[148,351,319],{"class":162},[148,353,355,357,359,361,364,366],{"class":150,"line":354},7,[148,356,302],{"class":301},[148,358,305],{"class":162},[148,360,177],{"class":169},[148,362,363],{"class":173},"4. Delete contact",[148,365,177],{"class":169},[148,367,319],{"class":162},[148,369,371,373,375,377,380,382],{"class":150,"line":370},8,[148,372,302],{"class":301},[148,374,305],{"class":162},[148,376,177],{"class":169},[148,378,379],{"class":173},"5. Exit",[148,381,177],{"class":169},[148,383,319],{"class":162},[148,385,387],{"class":150,"line":386},9,[148,388,284],{"emptyLinePlaceholder":283},[148,390,392,395,397,400,402,404,407,409],{"class":150,"line":391},10,[148,393,394],{"class":154},"    choice ",[148,396,159],{"class":158},[148,398,399],{"class":301}," input",[148,401,305],{"class":162},[148,403,177],{"class":169},[148,405,406],{"class":173},"Choose an option: ",[148,408,177],{"class":169},[148,410,319],{"class":162},[148,412,414],{"class":150,"line":413},11,[148,415,284],{"emptyLinePlaceholder":283},[148,417,419,422,425,428,430,433,435],{"class":150,"line":418},12,[148,420,421],{"class":289},"    if",[148,423,424],{"class":154}," choice ",[148,426,427],{"class":158},"==",[148,429,183],{"class":169},[148,431,432],{"class":173},"1",[148,434,177],{"class":169},[148,436,296],{"class":162},[148,438,440,443,445,447,449,451,454,456],{"class":150,"line":439},13,[148,441,442],{"class":154},"        name ",[148,444,159],{"class":158},[148,446,399],{"class":301},[148,448,305],{"class":162},[148,450,177],{"class":169},[148,452,453],{"class":173},"Name: ",[148,455,177],{"class":169},[148,457,319],{"class":162},[148,459,461,464,466,468,470,472,475,477],{"class":150,"line":460},14,[148,462,463],{"class":154},"        phone ",[148,465,159],{"class":158},[148,467,399],{"class":301},[148,469,305],{"class":162},[148,471,177],{"class":169},[148,473,474],{"class":173},"Phone: ",[148,476,177],{"class":169},[148,478,319],{"class":162},[148,480,482,485,488,491,494,497],{"class":150,"line":481},15,[148,483,484],{"class":154},"        contacts",[148,486,487],{"class":162},"[",[148,489,490],{"class":154},"name",[148,492,493],{"class":162},"]",[148,495,496],{"class":158}," =",[148,498,499],{"class":154}," phone\n",[148,501,503,506,508,510,513,515],{"class":150,"line":502},16,[148,504,505],{"class":301},"        print",[148,507,305],{"class":162},[148,509,177],{"class":169},[148,511,512],{"class":173},"Contact saved.",[148,514,177],{"class":169},[148,516,319],{"class":162},[148,518,520],{"class":150,"line":519},17,[148,521,284],{"emptyLinePlaceholder":283},[148,523,525,528,530,532,534,537,539],{"class":150,"line":524},18,[148,526,527],{"class":289},"    elif",[148,529,424],{"class":154},[148,531,427],{"class":158},[148,533,183],{"class":169},[148,535,536],{"class":173},"2",[148,538,177],{"class":169},[148,540,296],{"class":162},[148,542,544,547,550,553],{"class":150,"line":543},19,[148,545,546],{"class":289},"        if",[148,548,549],{"class":158}," not",[148,551,552],{"class":154}," contacts",[148,554,296],{"class":162},[148,556,558,561,563,565,568,570],{"class":150,"line":557},20,[148,559,560],{"class":301},"            print",[148,562,305],{"class":162},[148,564,177],{"class":169},[148,566,567],{"class":173},"No contacts found.",[148,569,177],{"class":169},[148,571,319],{"class":162},[148,573,575,578],{"class":150,"line":574},21,[148,576,577],{"class":289},"        else",[148,579,296],{"class":162},[148,581,583,586,589,592,595,598,600,602,606],{"class":150,"line":582},22,[148,584,585],{"class":289},"            for",[148,587,588],{"class":154}," name",[148,590,591],{"class":162},",",[148,593,594],{"class":154}," phone ",[148,596,597],{"class":289},"in",[148,599,552],{"class":154},[148,601,118],{"class":162},[148,603,605],{"class":604},"slqww","items",[148,607,608],{"class":162},"():\n",[148,610,612,615,617,619,621,623,626,628,630,633],{"class":150,"line":611},23,[148,613,614],{"class":301},"                print",[148,616,305],{"class":162},[148,618,490],{"class":604},[148,620,591],{"class":162},[148,622,183],{"class":169},[148,624,625],{"class":173},"-",[148,627,177],{"class":169},[148,629,591],{"class":162},[148,631,632],{"class":604}," phone",[148,634,319],{"class":162},[148,636,638],{"class":150,"line":637},24,[148,639,284],{"emptyLinePlaceholder":283},[148,641,643,645,647,649,651,654,656],{"class":150,"line":642},25,[148,644,527],{"class":289},[148,646,424],{"class":154},[148,648,427],{"class":158},[148,650,183],{"class":169},[148,652,653],{"class":173},"3",[148,655,177],{"class":169},[148,657,296],{"class":162},[148,659,661,663,665,667,669,671,674,676],{"class":150,"line":660},26,[148,662,442],{"class":154},[148,664,159],{"class":158},[148,666,399],{"class":301},[148,668,305],{"class":162},[148,670,177],{"class":169},[148,672,673],{"class":173},"Enter name to search: ",[148,675,177],{"class":169},[148,677,319],{"class":162},[148,679,681,683,686,688,690],{"class":150,"line":680},27,[148,682,546],{"class":289},[148,684,685],{"class":154}," name ",[148,687,597],{"class":158},[148,689,552],{"class":154},[148,691,296],{"class":162},[148,693,695,697,699,701,703,705,707,709,711,713,715,717],{"class":150,"line":694},28,[148,696,560],{"class":301},[148,698,305],{"class":162},[148,700,490],{"class":604},[148,702,591],{"class":162},[148,704,183],{"class":169},[148,706,625],{"class":173},[148,708,177],{"class":169},[148,710,591],{"class":162},[148,712,552],{"class":604},[148,714,487],{"class":162},[148,716,490],{"class":604},[148,718,719],{"class":162},"])\n",[148,721,723,725],{"class":150,"line":722},29,[148,724,577],{"class":289},[148,726,296],{"class":162},[148,728,730,732,734,736,739,741],{"class":150,"line":729},30,[148,731,560],{"class":301},[148,733,305],{"class":162},[148,735,177],{"class":169},[148,737,738],{"class":173},"Contact not found.",[148,740,177],{"class":169},[148,742,319],{"class":162},[148,744,746],{"class":150,"line":745},31,[148,747,284],{"emptyLinePlaceholder":283},[148,749,751,753,755,757,759,762,764],{"class":150,"line":750},32,[148,752,527],{"class":289},[148,754,424],{"class":154},[148,756,427],{"class":158},[148,758,183],{"class":169},[148,760,761],{"class":173},"4",[148,763,177],{"class":169},[148,765,296],{"class":162},[148,767,769,771,773,775,777,779,782,784],{"class":150,"line":768},33,[148,770,442],{"class":154},[148,772,159],{"class":158},[148,774,399],{"class":301},[148,776,305],{"class":162},[148,778,177],{"class":169},[148,780,781],{"class":173},"Enter name to delete: ",[148,783,177],{"class":169},[148,785,319],{"class":162},[148,787,789,791,793,795,797],{"class":150,"line":788},34,[148,790,546],{"class":289},[148,792,685],{"class":154},[148,794,597],{"class":158},[148,796,552],{"class":154},[148,798,296],{"class":162},[148,800,802,805,807,809,811],{"class":150,"line":801},35,[148,803,804],{"class":289},"            del",[148,806,552],{"class":154},[148,808,487],{"class":162},[148,810,490],{"class":154},[148,812,813],{"class":162},"]\n",[148,815,817,819,821,823,826,828],{"class":150,"line":816},36,[148,818,560],{"class":301},[148,820,305],{"class":162},[148,822,177],{"class":169},[148,824,825],{"class":173},"Contact deleted.",[148,827,177],{"class":169},[148,829,319],{"class":162},[148,831,833,835],{"class":150,"line":832},37,[148,834,577],{"class":289},[148,836,296],{"class":162},[148,838,840,842,844,846,848,850],{"class":150,"line":839},38,[148,841,560],{"class":301},[148,843,305],{"class":162},[148,845,177],{"class":169},[148,847,738],{"class":173},[148,849,177],{"class":169},[148,851,319],{"class":162},[148,853,855],{"class":150,"line":854},39,[148,856,284],{"emptyLinePlaceholder":283},[148,858,860,862,864,866,868,871,873],{"class":150,"line":859},40,[148,861,527],{"class":289},[148,863,424],{"class":154},[148,865,427],{"class":158},[148,867,183],{"class":169},[148,869,870],{"class":173},"5",[148,872,177],{"class":169},[148,874,296],{"class":162},[148,876,878,880,882,884,887,889],{"class":150,"line":877},41,[148,879,505],{"class":301},[148,881,305],{"class":162},[148,883,177],{"class":169},[148,885,886],{"class":173},"Goodbye!",[148,888,177],{"class":169},[148,890,319],{"class":162},[148,892,894],{"class":150,"line":893},42,[148,895,896],{"class":289},"        break\n",[148,898,900],{"class":150,"line":899},43,[148,901,284],{"emptyLinePlaceholder":283},[148,903,905,908],{"class":150,"line":904},44,[148,906,907],{"class":289},"    else",[148,909,296],{"class":162},[148,911,913,915,917,919,922,924],{"class":150,"line":912},45,[148,914,505],{"class":301},[148,916,305],{"class":162},[148,918,177],{"class":169},[148,920,921],{"class":173},"Invalid option.",[148,923,177],{"class":169},[148,925,319],{"class":162},[927,928,930],"h3",{"id":929},"how-this-works","How this works",[21,932,933,939,945,951,963],{},[24,934,935,938],{},[80,936,937],{},"contacts = {}"," creates an empty dictionary",[24,940,941,944],{},[80,942,943],{},"while True:"," creates a loop that keeps running",[24,946,947,950],{},[80,948,949],{},"choice = input(...)"," reads the menu option",[24,952,953,955,956,958,959,962],{},[80,954,99],{},", ",[80,957,103],{},", and ",[80,960,961],{},"else"," decide which action to run",[24,964,965,967],{},[80,966,260],{}," stops the loop when the user chooses to exit",[43,969,971],{"id":970},"add-a-new-contact","Add a new contact",[14,973,974],{},"To add a contact, ask for the name and phone number, then save it in the dictionary.",[139,976,978],{"className":141,"code":977,"language":143,"meta":144,"style":144},"name = input(\"Name: \")\nphone = input(\"Phone: \")\ncontacts[name] = phone\nprint(\"Contact saved.\")\n",[80,979,980,999,1018,1032],{"__ignoreMap":144},[148,981,982,985,987,989,991,993,995,997],{"class":150,"line":151},[148,983,984],{"class":154},"name ",[148,986,159],{"class":158},[148,988,399],{"class":301},[148,990,305],{"class":162},[148,992,177],{"class":169},[148,994,453],{"class":173},[148,996,177],{"class":169},[148,998,319],{"class":162},[148,1000,1001,1004,1006,1008,1010,1012,1014,1016],{"class":150,"line":166},[148,1002,1003],{"class":154},"phone ",[148,1005,159],{"class":158},[148,1007,399],{"class":301},[148,1009,305],{"class":162},[148,1011,177],{"class":169},[148,1013,474],{"class":173},[148,1015,177],{"class":169},[148,1017,319],{"class":162},[148,1019,1020,1022,1024,1026,1028,1030],{"class":150,"line":194},[148,1021,117],{"class":154},[148,1023,487],{"class":162},[148,1025,490],{"class":154},[148,1027,493],{"class":162},[148,1029,496],{"class":158},[148,1031,499],{"class":154},[148,1033,1034,1037,1039,1041,1043,1045],{"class":150,"line":214},[148,1035,1036],{"class":301},"print",[148,1038,305],{"class":162},[148,1040,177],{"class":169},[148,1042,512],{"class":173},[148,1044,177],{"class":169},[148,1046,319],{"class":162},[14,1048,1049],{},"Key line:",[139,1051,1053],{"className":141,"code":1052,"language":143,"meta":144,"style":144},"contacts[name] = phone\n",[80,1054,1055],{"__ignoreMap":144},[148,1056,1057,1059,1061,1063,1065,1067],{"class":150,"line":151},[148,1058,117],{"class":154},[148,1060,487],{"class":162},[148,1062,490],{"class":154},[148,1064,493],{"class":162},[148,1066,496],{"class":158},[148,1068,499],{"class":154},[14,1070,1071],{},"This stores the phone number under the contact name.",[14,1073,137],{},[139,1075,1077],{"className":141,"code":1076,"language":143,"meta":144,"style":144},"contacts[\"Alice\"] = \"1234567890\"\n",[80,1078,1079],{"__ignoreMap":144},[148,1080,1081,1083,1085,1087,1089,1091,1093,1095,1097,1099],{"class":150,"line":151},[148,1082,117],{"class":154},[148,1084,487],{"class":162},[148,1086,177],{"class":169},[148,1088,174],{"class":173},[148,1090,177],{"class":169},[148,1092,493],{"class":162},[148,1094,496],{"class":158},[148,1096,183],{"class":169},[148,1098,186],{"class":173},[148,1100,211],{"class":169},[14,1102,1103],{},"After that, the dictionary becomes:",[139,1105,1107],{"className":141,"code":1106,"language":143,"meta":144,"style":144},"{\"Alice\": \"1234567890\"}\n",[80,1108,1109],{"__ignoreMap":144},[148,1110,1111,1114,1116,1118,1120,1122,1124,1126,1128],{"class":150,"line":151},[148,1112,1113],{"class":162},"{",[148,1115,177],{"class":169},[148,1117,174],{"class":173},[148,1119,177],{"class":169},[148,1121,180],{"class":162},[148,1123,183],{"class":169},[148,1125,186],{"class":173},[148,1127,177],{"class":169},[148,1129,217],{"class":162},[14,1131,1132],{},"One important detail: if the same name is added again, the old phone number is replaced. That happens because dictionary keys must be unique.",[43,1134,1136],{"id":1135},"view-all-contacts","View all contacts",[14,1138,1139],{},"To show all contacts, first check whether the dictionary is empty.",[139,1141,1143],{"className":141,"code":1142,"language":143,"meta":144,"style":144},"if not contacts:\n    print(\"No contacts found.\")\nelse:\n    for name, phone in contacts.items():\n        print(name, \"-\", phone)\n",[80,1144,1145,1155,1169,1175,1196],{"__ignoreMap":144},[148,1146,1147,1149,1151,1153],{"class":150,"line":151},[148,1148,99],{"class":289},[148,1150,549],{"class":158},[148,1152,552],{"class":154},[148,1154,296],{"class":162},[148,1156,1157,1159,1161,1163,1165,1167],{"class":150,"line":166},[148,1158,302],{"class":301},[148,1160,305],{"class":162},[148,1162,177],{"class":169},[148,1164,567],{"class":173},[148,1166,177],{"class":169},[148,1168,319],{"class":162},[148,1170,1171,1173],{"class":150,"line":194},[148,1172,961],{"class":289},[148,1174,296],{"class":162},[148,1176,1177,1180,1182,1184,1186,1188,1190,1192,1194],{"class":150,"line":214},[148,1178,1179],{"class":289},"    for",[148,1181,588],{"class":154},[148,1183,591],{"class":162},[148,1185,594],{"class":154},[148,1187,597],{"class":289},[148,1189,552],{"class":154},[148,1191,118],{"class":162},[148,1193,605],{"class":604},[148,1195,608],{"class":162},[148,1197,1198,1200,1202,1204,1206,1208,1210,1212,1214,1216],{"class":150,"line":322},[148,1199,505],{"class":301},[148,1201,305],{"class":162},[148,1203,490],{"class":604},[148,1205,591],{"class":162},[148,1207,183],{"class":169},[148,1209,625],{"class":173},[148,1211,177],{"class":169},[148,1213,591],{"class":162},[148,1215,632],{"class":604},[148,1217,319],{"class":162},[14,1219,1220],{},"This does two things:",[21,1222,1223,1229],{},[24,1224,1225,1228],{},[80,1226,1227],{},"if not contacts:"," checks whether the dictionary is empty",[24,1230,1231,1234],{},[80,1232,1233],{},"contacts.items()"," gives you each name and phone number pair",[14,1236,1237,1238,1242,1243,118],{},"If you want more help with this pattern, see ",[88,1239,1241],{"href":1240},"\u002Fhow-to\u002Fhow-to-loop-through-a-dictionary-in-python\u002F","how to loop through a dictionary in Python"," and the ",[88,1244,1246,1249],{"href":1245},"\u002Freference\u002Fpython-dictionary-items-method\u002F",[80,1247,1248],{},"dict.items()"," method reference",[14,1251,1252],{},"Example output:",[139,1254,1259],{"className":1255,"code":1257,"language":1258,"meta":144},[1256],"language-text","Alice - 1234567890\nBob - 5551234567\n","text",[80,1260,1257],{"__ignoreMap":144},[43,1262,1264],{"id":1263},"search-for-a-contact-safely","Search for a contact safely",[14,1266,1267,1268,1271],{},"When searching, do not directly use ",[80,1269,1270],{},"contacts[name]"," unless you know the name exists.",[14,1273,1274],{},"Use a key check first:",[139,1276,1278],{"className":141,"code":1277,"language":143,"meta":144,"style":144},"name = input(\"Enter name to search: \")\n\nif name in contacts:\n    print(name, \"-\", contacts[name])\nelse:\n    print(\"Contact not found.\")\n",[80,1279,1280,1298,1302,1314,1340,1346],{"__ignoreMap":144},[148,1281,1282,1284,1286,1288,1290,1292,1294,1296],{"class":150,"line":151},[148,1283,984],{"class":154},[148,1285,159],{"class":158},[148,1287,399],{"class":301},[148,1289,305],{"class":162},[148,1291,177],{"class":169},[148,1293,673],{"class":173},[148,1295,177],{"class":169},[148,1297,319],{"class":162},[148,1299,1300],{"class":150,"line":166},[148,1301,284],{"emptyLinePlaceholder":283},[148,1303,1304,1306,1308,1310,1312],{"class":150,"line":194},[148,1305,99],{"class":289},[148,1307,685],{"class":154},[148,1309,597],{"class":158},[148,1311,552],{"class":154},[148,1313,296],{"class":162},[148,1315,1316,1318,1320,1322,1324,1326,1328,1330,1332,1334,1336,1338],{"class":150,"line":214},[148,1317,302],{"class":301},[148,1319,305],{"class":162},[148,1321,490],{"class":604},[148,1323,591],{"class":162},[148,1325,183],{"class":169},[148,1327,625],{"class":173},[148,1329,177],{"class":169},[148,1331,591],{"class":162},[148,1333,552],{"class":604},[148,1335,487],{"class":162},[148,1337,490],{"class":604},[148,1339,719],{"class":162},[148,1341,1342,1344],{"class":150,"line":322},[148,1343,961],{"class":289},[148,1345,296],{"class":162},[148,1347,1348,1350,1352,1354,1356,1358],{"class":150,"line":338},[148,1349,302],{"class":301},[148,1351,305],{"class":162},[148,1353,177],{"class":169},[148,1355,738],{"class":173},[148,1357,177],{"class":169},[148,1359,319],{"class":162},[14,1361,1362,1363,118],{},"This avoids a ",[80,1364,1365],{},"KeyError",[14,1367,1368],{},"Why this matters:",[21,1370,1371,1374],{},[24,1372,1373],{},"If the name exists, you can safely read the phone number",[24,1375,1376],{},"If the name does not exist, the program prints a clear message instead of crashing",[14,1378,1379,1380,100,1384,118],{},"If you want more detail, see ",[88,1381,1383],{"href":1382},"\u002Fhow-to\u002Fhow-to-check-if-a-key-exists-in-a-dictionary-in-python\u002F","how to check if a key exists in a dictionary in Python",[88,1385,1387],{"href":1386},"\u002Ferrors\u002Fkeyerror-in-python-causes-and-fixes\u002F","KeyError in Python: causes and fixes",[43,1389,1391],{"id":1390},"delete-a-contact-safely","Delete a contact safely",[14,1393,1394],{},"Deleting follows the same idea as searching.",[14,1396,1397],{},"Check first, then delete:",[139,1399,1401],{"className":141,"code":1400,"language":143,"meta":144,"style":144},"name = input(\"Enter name to delete: \")\n\nif name in contacts:\n    del contacts[name]\n    print(\"Contact deleted.\")\nelse:\n    print(\"Contact not found.\")\n",[80,1402,1403,1421,1425,1437,1450,1464,1470],{"__ignoreMap":144},[148,1404,1405,1407,1409,1411,1413,1415,1417,1419],{"class":150,"line":151},[148,1406,984],{"class":154},[148,1408,159],{"class":158},[148,1410,399],{"class":301},[148,1412,305],{"class":162},[148,1414,177],{"class":169},[148,1416,781],{"class":173},[148,1418,177],{"class":169},[148,1420,319],{"class":162},[148,1422,1423],{"class":150,"line":166},[148,1424,284],{"emptyLinePlaceholder":283},[148,1426,1427,1429,1431,1433,1435],{"class":150,"line":194},[148,1428,99],{"class":289},[148,1430,685],{"class":154},[148,1432,597],{"class":158},[148,1434,552],{"class":154},[148,1436,296],{"class":162},[148,1438,1439,1442,1444,1446,1448],{"class":150,"line":214},[148,1440,1441],{"class":289},"    del",[148,1443,552],{"class":154},[148,1445,487],{"class":162},[148,1447,490],{"class":154},[148,1449,813],{"class":162},[148,1451,1452,1454,1456,1458,1460,1462],{"class":150,"line":322},[148,1453,302],{"class":301},[148,1455,305],{"class":162},[148,1457,177],{"class":169},[148,1459,825],{"class":173},[148,1461,177],{"class":169},[148,1463,319],{"class":162},[148,1465,1466,1468],{"class":150,"line":338},[148,1467,961],{"class":289},[148,1469,296],{"class":162},[148,1471,1472,1474,1476,1478,1480,1482],{"class":150,"line":354},[148,1473,302],{"class":301},[148,1475,305],{"class":162},[148,1477,177],{"class":169},[148,1479,738],{"class":173},[148,1481,177],{"class":169},[148,1483,319],{"class":162},[14,1485,1049],{},[139,1487,1489],{"className":141,"code":1488,"language":143,"meta":144,"style":144},"del contacts[name]\n",[80,1490,1491],{"__ignoreMap":144},[148,1492,1493,1496,1498,1500,1502],{"class":150,"line":151},[148,1494,1495],{"class":289},"del",[148,1497,552],{"class":154},[148,1499,487],{"class":162},[148,1501,490],{"class":154},[148,1503,813],{"class":162},[14,1505,1506],{},"This removes the contact from the dictionary, but only if the name exists.",[43,1508,1510],{"id":1509},"common-beginner-improvements","Common beginner improvements",[14,1512,1513],{},"Once the basic version works, you can improve it in simple ways:",[21,1515,1516,1519,1522,1525,1528],{},[24,1517,1518],{},"Store more than one field per contact",[24,1520,1521],{},"Use nested dictionaries for phone and email",[24,1523,1524],{},"Make name search case-insensitive",[24,1526,1527],{},"Sort contacts before printing",[24,1529,1530],{},"Save contacts to a file later",[14,1532,1533],{},"For example, a contact could look like this:",[139,1535,1537],{"className":141,"code":1536,"language":143,"meta":144,"style":144},"contacts = {\n    \"Alice\": {\n        \"phone\": \"1234567890\",\n        \"email\": \"alice@example.com\"\n    }\n}\n",[80,1538,1539,1547,1559,1579,1597,1602],{"__ignoreMap":144},[148,1540,1541,1543,1545],{"class":150,"line":151},[148,1542,155],{"class":154},[148,1544,159],{"class":158},[148,1546,163],{"class":162},[148,1548,1549,1551,1553,1555,1557],{"class":150,"line":166},[148,1550,170],{"class":169},[148,1552,174],{"class":173},[148,1554,177],{"class":169},[148,1556,180],{"class":162},[148,1558,163],{"class":162},[148,1560,1561,1564,1567,1569,1571,1573,1575,1577],{"class":150,"line":194},[148,1562,1563],{"class":169},"        \"",[148,1565,1566],{"class":173},"phone",[148,1568,177],{"class":169},[148,1570,180],{"class":162},[148,1572,183],{"class":169},[148,1574,186],{"class":173},[148,1576,177],{"class":169},[148,1578,191],{"class":162},[148,1580,1581,1583,1586,1588,1590,1592,1595],{"class":150,"line":214},[148,1582,1563],{"class":169},[148,1584,1585],{"class":173},"email",[148,1587,177],{"class":169},[148,1589,180],{"class":162},[148,1591,183],{"class":169},[148,1593,1594],{"class":173},"alice@example.com",[148,1596,211],{"class":169},[148,1598,1599],{"class":150,"line":322},[148,1600,1601],{"class":162},"    }\n",[148,1603,1604],{"class":150,"line":338},[148,1605,217],{"class":162},[14,1607,1608,1609,118],{},"A useful next step is saving your contacts to a JSON file. See ",[88,1610,1612],{"href":1611},"\u002Fhow-to\u002Fhow-to-parse-json-in-python\u002F","how to parse JSON in Python",[43,1614,1616],{"id":1615},"beginner-debugging-tips","Beginner debugging tips",[14,1618,1619],{},"If your program does not behave as expected, test one small part at a time.",[14,1621,1622],{},"Helpful debug lines:",[139,1624,1626],{"className":141,"code":1625,"language":143,"meta":144,"style":144},"print(contacts)\nprint(choice)\nprint(type(choice))\nprint(name in contacts)\nfor name, phone in contacts.items():\n    print(name, phone)\n",[80,1627,1628,1638,1649,1666,1680,1701],{"__ignoreMap":144},[148,1629,1630,1632,1634,1636],{"class":150,"line":151},[148,1631,1036],{"class":301},[148,1633,305],{"class":162},[148,1635,117],{"class":604},[148,1637,319],{"class":162},[148,1639,1640,1642,1644,1647],{"class":150,"line":166},[148,1641,1036],{"class":301},[148,1643,305],{"class":162},[148,1645,1646],{"class":604},"choice",[148,1648,319],{"class":162},[148,1650,1651,1653,1655,1659,1661,1663],{"class":150,"line":194},[148,1652,1036],{"class":301},[148,1654,305],{"class":162},[148,1656,1658],{"class":1657},"sZMiF","type",[148,1660,305],{"class":162},[148,1662,1646],{"class":604},[148,1664,1665],{"class":162},"))\n",[148,1667,1668,1670,1672,1674,1676,1678],{"class":150,"line":214},[148,1669,1036],{"class":301},[148,1671,305],{"class":162},[148,1673,984],{"class":604},[148,1675,597],{"class":289},[148,1677,552],{"class":604},[148,1679,319],{"class":162},[148,1681,1682,1685,1687,1689,1691,1693,1695,1697,1699],{"class":150,"line":322},[148,1683,1684],{"class":289},"for",[148,1686,588],{"class":154},[148,1688,591],{"class":162},[148,1690,594],{"class":154},[148,1692,597],{"class":289},[148,1694,552],{"class":154},[148,1696,118],{"class":162},[148,1698,605],{"class":604},[148,1700,608],{"class":162},[148,1702,1703,1705,1707,1709,1711,1713],{"class":150,"line":338},[148,1704,302],{"class":301},[148,1706,305],{"class":162},[148,1708,490],{"class":604},[148,1710,591],{"class":162},[148,1712,632],{"class":604},[148,1714,319],{"class":162},[14,1716,1717],{},"These can help you check:",[21,1719,1720,1723,1726,1732,1735],{},[24,1721,1722],{},"whether contacts are being stored correctly",[24,1724,1725],{},"what the user actually entered",[24,1727,1728,1729,1731],{},"whether ",[80,1730,1646],{}," is a string",[24,1733,1734],{},"whether a name exists in the dictionary",[24,1736,1737],{},"whether the loop is reading your data correctly",[43,1739,1741],{"id":1740},"common-mistakes","Common mistakes",[14,1743,1744],{},"Here are some common beginner problems in this project.",[927,1746,86,1748,1750],{"id":1747},"using-contactsname-before-checking-if-the-name-exists",[80,1749,1270],{}," before checking if the name exists",[14,1752,1753,1754,118],{},"This can raise a ",[80,1755,1365],{},[14,1757,1758],{},"Problem example:",[139,1760,1762],{"className":141,"code":1761,"language":143,"meta":144,"style":144},"name = input(\"Enter name: \")\nprint(contacts[name])\n",[80,1763,1764,1783],{"__ignoreMap":144},[148,1765,1766,1768,1770,1772,1774,1776,1779,1781],{"class":150,"line":151},[148,1767,984],{"class":154},[148,1769,159],{"class":158},[148,1771,399],{"class":301},[148,1773,305],{"class":162},[148,1775,177],{"class":169},[148,1777,1778],{"class":173},"Enter name: ",[148,1780,177],{"class":169},[148,1782,319],{"class":162},[148,1784,1785,1787,1789,1791,1793,1795],{"class":150,"line":166},[148,1786,1036],{"class":301},[148,1788,305],{"class":162},[148,1790,117],{"class":604},[148,1792,487],{"class":162},[148,1794,490],{"class":604},[148,1796,719],{"class":162},[14,1798,1799],{},"Safer version:",[139,1801,1803],{"className":141,"code":1802,"language":143,"meta":144,"style":144},"if name in contacts:\n    print(contacts[name])\nelse:\n    print(\"Contact not found.\")\n",[80,1804,1805,1817,1831,1837],{"__ignoreMap":144},[148,1806,1807,1809,1811,1813,1815],{"class":150,"line":151},[148,1808,99],{"class":289},[148,1810,685],{"class":154},[148,1812,597],{"class":158},[148,1814,552],{"class":154},[148,1816,296],{"class":162},[148,1818,1819,1821,1823,1825,1827,1829],{"class":150,"line":166},[148,1820,302],{"class":301},[148,1822,305],{"class":162},[148,1824,117],{"class":604},[148,1826,487],{"class":162},[148,1828,490],{"class":604},[148,1830,719],{"class":162},[148,1832,1833,1835],{"class":150,"line":194},[148,1834,961],{"class":289},[148,1836,296],{"class":162},[148,1838,1839,1841,1843,1845,1847,1849],{"class":150,"line":214},[148,1840,302],{"class":301},[148,1842,305],{"class":162},[148,1844,177],{"class":169},[148,1846,738],{"class":173},[148,1848,177],{"class":169},[148,1850,319],{"class":162},[927,1852,1854,1855,1857],{"id":1853},"forgetting-that-input-returns-a-string","Forgetting that ",[80,1856,93],{}," returns a string",[14,1859,1860],{},"This is a very common issue.",[14,1862,1758],{},[139,1864,1866],{"className":141,"code":1865,"language":143,"meta":144,"style":144},"choice = input(\"Choose an option: \")\n\nif choice == 1:\n    print(\"Add contact\")\n",[80,1867,1868,1887,1891,1905],{"__ignoreMap":144},[148,1869,1870,1873,1875,1877,1879,1881,1883,1885],{"class":150,"line":151},[148,1871,1872],{"class":154},"choice ",[148,1874,159],{"class":158},[148,1876,399],{"class":301},[148,1878,305],{"class":162},[148,1880,177],{"class":169},[148,1882,406],{"class":173},[148,1884,177],{"class":169},[148,1886,319],{"class":162},[148,1888,1889],{"class":150,"line":166},[148,1890,284],{"emptyLinePlaceholder":283},[148,1892,1893,1895,1897,1899,1903],{"class":150,"line":194},[148,1894,99],{"class":289},[148,1896,424],{"class":154},[148,1898,427],{"class":158},[148,1900,1902],{"class":1901},"srdBf"," 1",[148,1904,296],{"class":162},[148,1906,1907,1909,1911,1913,1916,1918],{"class":150,"line":214},[148,1908,302],{"class":301},[148,1910,305],{"class":162},[148,1912,177],{"class":169},[148,1914,1915],{"class":173},"Add contact",[148,1917,177],{"class":169},[148,1919,319],{"class":162},[14,1921,1922,1923,1925],{},"This does not match because ",[80,1924,1646],{}," is a string, not a number.",[14,1927,1928],{},"Correct version:",[139,1930,1932],{"className":141,"code":1931,"language":143,"meta":144,"style":144},"if choice == \"1\":\n    print(\"Add contact\")\n",[80,1933,1934,1950],{"__ignoreMap":144},[148,1935,1936,1938,1940,1942,1944,1946,1948],{"class":150,"line":151},[148,1937,99],{"class":289},[148,1939,424],{"class":154},[148,1941,427],{"class":158},[148,1943,183],{"class":169},[148,1945,432],{"class":173},[148,1947,177],{"class":169},[148,1949,296],{"class":162},[148,1951,1952,1954,1956,1958,1960,1962],{"class":150,"line":166},[148,1953,302],{"class":301},[148,1955,305],{"class":162},[148,1957,177],{"class":169},[148,1959,1915],{"class":173},[148,1961,177],{"class":169},[148,1963,319],{"class":162},[14,1965,1966],{},"You can confirm this with:",[139,1968,1970],{"className":141,"code":1969,"language":143,"meta":144,"style":144},"print(type(choice))\n",[80,1971,1972],{"__ignoreMap":144},[148,1973,1974,1976,1978,1980,1982,1984],{"class":150,"line":151},[148,1975,1036],{"class":301},[148,1977,305],{"class":162},[148,1979,1658],{"class":1657},[148,1981,305],{"class":162},[148,1983,1646],{"class":604},[148,1985,1665],{"class":162},[927,1987,1989,1990,1992],{"id":1988},"indentation-mistakes-inside-the-while-loop","Indentation mistakes inside the ",[80,1991,82],{}," loop",[14,1994,1995],{},"Python uses indentation to define blocks of code.",[14,1997,1998],{},"For example, this is wrong:",[139,2000,2002],{"className":141,"code":2001,"language":143,"meta":144,"style":144},"while True:\nprint(\"Menu\")\n",[80,2003,2004,2012],{"__ignoreMap":144},[148,2005,2006,2008,2010],{"class":150,"line":151},[148,2007,82],{"class":289},[148,2009,293],{"class":292},[148,2011,296],{"class":162},[148,2013,2014,2016,2018,2020,2023,2025],{"class":150,"line":166},[148,2015,1036],{"class":301},[148,2017,305],{"class":162},[148,2019,177],{"class":169},[148,2021,2022],{"class":173},"Menu",[148,2024,177],{"class":169},[148,2026,319],{"class":162},[14,2028,2029],{},"This causes an indentation error.",[14,2031,1928],{},[139,2033,2035],{"className":141,"code":2034,"language":143,"meta":144,"style":144},"while True:\n    print(\"Menu\")\n",[80,2036,2037,2045],{"__ignoreMap":144},[148,2038,2039,2041,2043],{"class":150,"line":151},[148,2040,82],{"class":289},[148,2042,293],{"class":292},[148,2044,296],{"class":162},[148,2046,2047,2049,2051,2053,2055,2057],{"class":150,"line":166},[148,2048,302],{"class":301},[148,2050,305],{"class":162},[148,2052,177],{"class":169},[148,2054,2022],{"class":173},[148,2056,177],{"class":169},[148,2058,319],{"class":162},[14,2060,2061,2062,118],{},"If you see this problem, read ",[88,2063,2065],{"href":2064},"\u002Ferrors\u002Findentationerror-expected-an-indented-block-fix\u002F","IndentationError: expected an indented block",[927,2067,2069],{"id":2068},"overwriting-a-contact-by-using-the-same-name-again","Overwriting a contact by using the same name again",[14,2071,2072],{},"This is valid Python behavior, but beginners sometimes do not expect it.",[14,2074,137],{},[139,2076,2078],{"className":141,"code":2077,"language":143,"meta":144,"style":144},"contacts[\"Alice\"] = \"1111111111\"\ncontacts[\"Alice\"] = \"2222222222\"\n\nprint(contacts)\n",[80,2079,2080,2103,2126,2130],{"__ignoreMap":144},[148,2081,2082,2084,2086,2088,2090,2092,2094,2096,2098,2101],{"class":150,"line":151},[148,2083,117],{"class":154},[148,2085,487],{"class":162},[148,2087,177],{"class":169},[148,2089,174],{"class":173},[148,2091,177],{"class":169},[148,2093,493],{"class":162},[148,2095,496],{"class":158},[148,2097,183],{"class":169},[148,2099,2100],{"class":173},"1111111111",[148,2102,211],{"class":169},[148,2104,2105,2107,2109,2111,2113,2115,2117,2119,2121,2124],{"class":150,"line":166},[148,2106,117],{"class":154},[148,2108,487],{"class":162},[148,2110,177],{"class":169},[148,2112,174],{"class":173},[148,2114,177],{"class":169},[148,2116,493],{"class":162},[148,2118,496],{"class":158},[148,2120,183],{"class":169},[148,2122,2123],{"class":173},"2222222222",[148,2125,211],{"class":169},[148,2127,2128],{"class":150,"line":194},[148,2129,284],{"emptyLinePlaceholder":283},[148,2131,2132,2134,2136,2138],{"class":150,"line":214},[148,2133,1036],{"class":301},[148,2135,305],{"class":162},[148,2137,117],{"class":604},[148,2139,319],{"class":162},[14,2141,2142],{},"Output:",[139,2144,2146],{"className":141,"code":2145,"language":143,"meta":144,"style":144},"{'Alice': '2222222222'}\n",[80,2147,2148],{"__ignoreMap":144},[148,2149,2150,2152,2155,2157,2159,2161,2164,2166,2168],{"class":150,"line":151},[148,2151,1113],{"class":162},[148,2153,2154],{"class":169},"'",[148,2156,174],{"class":173},[148,2158,2154],{"class":169},[148,2160,180],{"class":162},[148,2162,2163],{"class":169}," '",[148,2165,2123],{"class":173},[148,2167,2154],{"class":169},[148,2169,217],{"class":162},[14,2171,2172],{},"The second value replaces the first one.",[927,2174,86,2176,2179,2180],{"id":2175},"using-contactsitems-instead-of-contactsitems",[80,2177,2178],{},"contacts.items"," instead of ",[80,2181,1233],{},[14,2183,2184],{},"This is another common mistake.",[14,2186,2187],{},"Wrong:",[139,2189,2191],{"className":141,"code":2190,"language":143,"meta":144,"style":144},"for name, phone in contacts.items:\n    print(name, phone)\n",[80,2192,2193,2214],{"__ignoreMap":144},[148,2194,2195,2197,2199,2201,2203,2205,2207,2209,2212],{"class":150,"line":151},[148,2196,1684],{"class":289},[148,2198,588],{"class":154},[148,2200,591],{"class":162},[148,2202,594],{"class":154},[148,2204,597],{"class":289},[148,2206,552],{"class":154},[148,2208,118],{"class":162},[148,2210,605],{"class":2211},"skxfh",[148,2213,296],{"class":162},[148,2215,2216,2218,2220,2222,2224,2226],{"class":150,"line":166},[148,2217,302],{"class":301},[148,2219,305],{"class":162},[148,2221,490],{"class":604},[148,2223,591],{"class":162},[148,2225,632],{"class":604},[148,2227,319],{"class":162},[14,2229,2230],{},"Correct:",[139,2232,2234],{"className":141,"code":2233,"language":143,"meta":144,"style":144},"for name, phone in contacts.items():\n    print(name, phone)\n",[80,2235,2236,2256],{"__ignoreMap":144},[148,2237,2238,2240,2242,2244,2246,2248,2250,2252,2254],{"class":150,"line":151},[148,2239,1684],{"class":289},[148,2241,588],{"class":154},[148,2243,591],{"class":162},[148,2245,594],{"class":154},[148,2247,597],{"class":289},[148,2249,552],{"class":154},[148,2251,118],{"class":162},[148,2253,605],{"class":604},[148,2255,608],{"class":162},[148,2257,2258,2260,2262,2264,2266,2268],{"class":150,"line":166},[148,2259,302],{"class":301},[148,2261,305],{"class":162},[148,2263,490],{"class":604},[148,2265,591],{"class":162},[148,2267,632],{"class":604},[148,2269,319],{"class":162},[14,2271,2272,2273,2276],{},"The parentheses are required because ",[80,2274,2275],{},"items()"," is a method.",[43,2278,2280],{"id":2279},"faq","FAQ",[927,2282,2284],{"id":2283},"why-is-a-dictionary-used-for-this-project","Why is a dictionary used for this project?",[14,2286,2287],{},"A dictionary lets you look up a contact by name quickly and keeps the example simple.",[927,2289,2291],{"id":2290},"what-happens-if-i-add-the-same-name-twice","What happens if I add the same name twice?",[14,2293,2294],{},"The new phone number replaces the old value because dictionary keys must be unique.",[927,2296,2298],{"id":2297},"can-i-store-email-addresses-too","Can I store email addresses too?",[14,2300,2301],{},"Yes. A good next step is to store each contact as a nested dictionary with fields like phone and email.",[927,2303,2305],{"id":2304},"why-does-my-menu-choice-not-match","Why does my menu choice not match?",[14,2307,2308,2310,2311,2314,2315,118],{},[80,2309,93],{}," returns text, so compare against string values like ",[80,2312,2313],{},"\"1\""," instead of the number ",[80,2316,432],{},[927,2318,2320],{"id":2319},"how-can-i-save-contacts-after-the-program-closes","How can I save contacts after the program closes?",[14,2322,2323],{},"A good next improvement is saving the dictionary to a file, such as JSON.",[43,2325,2327],{"id":2326},"see-also","See also",[21,2329,2330,2334,2339,2344,2349,2357,2361],{},[24,2331,2332],{},[88,2333,227],{"href":226},[24,2335,2336],{},[88,2337,2338],{"href":90},"How to get user input in Python",[24,2340,2341],{},[88,2342,2343],{"href":1240},"How to loop through a dictionary in Python",[24,2345,2346],{},[88,2347,2348],{"href":1382},"How to check if a key exists in a dictionary in Python",[24,2350,2351],{},[88,2352,2353,2354,2356],{"href":1245},"Python ",[80,2355,1248],{}," method",[24,2358,2359],{},[88,2360,1387],{"href":1386},[24,2362,2363],{},[88,2364,2366],{"href":2365},"\u002Fexamples\u002Fpython-to-do-list-script-example\u002F","Python to-do list script example",[14,2368,2369],{},"After you build the basic contact book, try improving it by adding email fields, duplicate checks, sorting, and file saving with JSON.",[2371,2372,2373],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":144,"searchDepth":166,"depth":166,"links":2375},[2376,2377,2378,2379,2382,2383,2384,2385,2386,2387,2388,2399,2406],{"id":45,"depth":166,"text":46},{"id":66,"depth":166,"text":67},{"id":110,"depth":166,"text":111},{"id":230,"depth":166,"text":231,"children":2380},[2381],{"id":929,"depth":194,"text":930},{"id":970,"depth":166,"text":971},{"id":1135,"depth":166,"text":1136},{"id":1263,"depth":166,"text":1264},{"id":1390,"depth":166,"text":1391},{"id":1509,"depth":166,"text":1510},{"id":1615,"depth":166,"text":1616},{"id":1740,"depth":166,"text":1741,"children":2389},[2390,2392,2394,2396,2397],{"id":1747,"depth":194,"text":2391},"Using contacts[name] before checking if the name exists",{"id":1853,"depth":194,"text":2393},"Forgetting that input() returns a string",{"id":1988,"depth":194,"text":2395},"Indentation mistakes inside the while loop",{"id":2068,"depth":194,"text":2069},{"id":2175,"depth":194,"text":2398},"Using contacts.items instead of contacts.items()",{"id":2279,"depth":166,"text":2280,"children":2400},[2401,2402,2403,2404,2405],{"id":2283,"depth":194,"text":2284},{"id":2290,"depth":194,"text":2291},{"id":2297,"depth":194,"text":2298},{"id":2304,"depth":194,"text":2305},{"id":2319,"depth":194,"text":2320},{"id":2326,"depth":166,"text":2327},"Master python contact book example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-contact-book-example",{"title":5,"description":2407},"examples\u002Fpython-contact-book-example","q5NNl2Lrh33VN1ZLB4J3uifWOSWE5eVcf2FQRre5C1I",1777585474932]