So that even if you type only a part of the name, the corresponding phone number is printed out.

Asked 2 years ago, Updated 2 years ago, 14 views

//
dic={'Hong Gil-dong':'010-4444-5555','Kim Jungang':'010-9191-8181','Simcheong':'010-3232-5454'}


while True:
    user = input ('name>>')

    namelist = list(dic.keys())
    numberlist = list(dic.values())
    if user in namelist:
        print(user, dic[user])
    else:
        if user == 'add':
            newname = input ("What is your name?")
            newnum = input ("What's the phone number?" ")
            dic[newname] = newnum
            print(newname, "Phone number added.")
        else:
            print("Not Found".")
    continue

I made this code.

Enter 'Hong Gil-dong' to display the value of the dictionary.

By the way, how can I get it just by searching for "Honggil"?

python

2022-09-20 17:04

1 Answers

I think we need to compare the namelist elements with the user value in a repetitive sentence.

for i in namelist:
    if i.find(user) >= 0:
        print(numberlist.__getitem__(namelist.index(i)))

If you do this, you can find even one letter overlapping, but if the name overlaps even one letter, you will need to take care of it accordingly. I think you'll be able to study this part a lot.


2022-09-20 17:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.