0
from time import sleep

emergency = "911"
phone = {"Mother":"xxx-xxx-xxxx", "Father":"xxx-xxx-xxxx", "911":emergency,
"Police":emergency, "Fire":emergency, "Emergency":emergency}

emails = {"Mother":"xxxxxx@gmail.com", "Father":"xxxxxx@gmail.com", "911":emergency, "Police":emergency, "Fire":emergency, "Emergency":emergency}


while True:
    email_numb = input("Do you want Email or Phone numbers? (Type one, Phone or Email) For more options type m: ").lower().capitalize()

    if email_numb == "Email":
        email_input = input("Who's email would you like to find? (enter a name): ").lower().capitalize()
        if email_input in emails:
            print(emails[email_input])
        else:
           print("Sorry but I cannot find the name, check out for typos")
        sleep(2)

    elif email_numb == "Phone":
        phone_input = input("Who's phone number would you like to find? (enter a name): ").lower().capitalize()
        if phone_input in phone:
            print(phone[phone_input])
        else:
            print("Sorry but I cannot find the name, check for typos")
        sleep(2)
    
    elif email_numb == "M":
        what_to_do = input("Do want to edit, add, or delete, a person in the directory? (Type add, edit, or delete) ").lower().capitalize()
        if what_to_do == "Add":
            email_number = input("Would you like to add a new email address or a new phone number? (type email or phone) ").lower().capitalize()
            if email_number == "Email":
                name = input("Please type the name: ")
                email = input("Please type the email: ")
                print(f"{name}, {email}")
                emails.update({name:email})
                print("Added succesfully!")
                continue
    elif email_numb == "Q":
        break

    elif email_numb != {'Email', 'Phone', "M"}  :
        print("Sorry, I don't understand")
        sleep(2)
        continue
        

    stop_or_continue = input("Would you like to search up another number/email address? Press q to quit and any other key to continue: ")

    if stop_or_continue == "q":
        break
    else:
        continue

I type "m", then "add", I put in the name, then the email, (I only programmed that so far, I didn't do phone numbers yet), it doesn't give me errors, but it doesn't add the name or email to the dictionary. I know I should organize the code, but does anyone know why it doesn't work?

  • And how do you know it doesn't work? You never output the `emails` dictionary in your code. – Daniil Fajnberg Aug 23 '22 at 18:47
  • and if i put ` if name and email in emails:` `print("Added succesfully!")` then it doesn't print "Added succesfully!" – Aharon Becker Aug 23 '22 at 18:48
  • It works for me. I debugged using PyCharm, stepped through the code, and right after the line `emails.update({name:email})` I checked the `emails` dict and the data was there. – Random Davis Aug 23 '22 at 18:49
  • @AharonBecker Of course `if name and email in emails` won't work, that's not the correct way to check if something is in a dict. – Random Davis Aug 23 '22 at 18:49
  • really? im using visual studio code and it doesn't work. Should i try with pycharm? – Aharon Becker Aug 23 '22 at 18:50
  • Does this answer your question? [How to check if a key-value pair is present in a dictionary?](https://stackoverflow.com/questions/34273544/how-to-check-if-a-key-value-pair-is-present-in-a-dictionary) – Random Davis Aug 23 '22 at 18:51
  • @Random Davis but the code doesn't add the name and email to dict whether or not it use that code – Aharon Becker Aug 23 '22 at 18:51
  • When accepting entries for the new name and email you don't do `.lower().capitalize()`. However when searching, you do. – quamrana Aug 23 '22 at 18:51
  • @AharonBecker then the code you shared is not the code you are using, since I followed your exact steps and the key and value are definitely present in the dict. I used the debugger to inspect the dict directly. Maybe you're checking it wrong. [This](https://stackoverflow.com/a/34273568/6273251) is how to check if a key-value pair is present in a dict. Basically you just do `key in a and value == a[key]`. – Random Davis Aug 23 '22 at 18:51
  • I changed the line to: `name = input("Please type the name: ").lower().capitalize()` and it works now. – quamrana Aug 23 '22 at 18:53
  • @Random Davis i don't understand. I will copy paste code from question into my ide even though i don't think thatll help – Aharon Becker Aug 23 '22 at 18:55
  • It didn't work. – Aharon Becker Aug 23 '22 at 18:57

0 Answers0