# 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

Edited 5 Years Ago by Narue: Added code tags

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'

Edited 5 Years Ago by pyTony: n/a

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

Edited 5 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

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

Edited 5 Years Ago by pyguy62: n/a

This article has been dead for over six months. Start a new discussion instead.