# phonebook dictionary
phonebook={'francis':'francis@gmail.com','iris': 'iris@gmail.com','karen':'karen@gmail.com'}

name=raw_input('what name you want to check  :')

# check if the name is in the dictionary
if name in phonebook.items() :
    name=key
    print phonebook.get(key)  # if yes, print the email
else:
    print 'not here'
    

# check the correctness of the program
# I could not get the email address after keying in the name

# please help, thanks

Recommended Answers

All 5 Replies

See my signarture linked post on some hints on using this forum, especially how to use code tags.

# phonebook dictionary
phonebook={'francis':'francis@gmail.com','iris': 'iris@gmail.com','karen':'karen@gmail.com'}

name=raw_input('what name you want to check  :')
# what you are checking
print(phonebook.items())
# check if the name is in the dictionary
if name in phonebook.items() :
    name=key
    print phonebook.get(key)  # if yes, print the email
else:
    print 'not here'

print phonebook[name] if name in phonebook else 'not here'

ohhh tony and his one-liners ;-)

also, I'm not sure why you thought checking phonebook.items was the most prudent style...I hope you already know what's wrong with that choice; why not phonebook.keys()?

# phonebook dictionary
phonebook={'francis':'francis@gmail.com','iris': 'iris@gmail.com','karen':'karen@gmail.com'}

name=raw_input('what name you want to check  :')
# what you are checking

# check if the name is in the dictionary
if name in phonebook.keys() :
    print phonebook.get(name)  # if yes, print the email
else:
    print 'not here'

Thanks pyguy62
Though I have to struggle a bit. Good if you explain more

ohhh tony and his one-liners ;-)

also, I'm not sure why you thought checking phonebook.items was the most prudent style...I hope you already know what's wrong with that choice; why not phonebook.keys()?

Hi,

Thanks,

Better if you would explain more

well when you call items, you get just that, all the items; keys and values. when you call keys you just get the keys and from there you could say something like:

query=input('Enter your search: ')
if query in phonebook.keys():
   print(phonebook[query])
else:
   print('invalid selection')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.