Home > database >  Is there something I am doing wrong in this code? The add is not working
Is there something I am doing wrong in this code? The add is not working

Time:08-24

There seems to be something wrong with my code but I can't figure it out.

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":"[email protected]", "Father":"[email protected]",
"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()

    elif 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":
            new_email = []
            name = input("Please type the name: ")
            new_email.append(name)
            email = input("Please type the email: ")
            new_email.append(email)
            print(new_email)
            new_pair = ":".join(new_email)
            emails.update({new_pair})
            continue

    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

When I try to type in add it doesn't work, it just skips to the stop_or_continue input. And somwetimes when I type in random things it goes to the part where I can put in the name and email. it was working perfectly fine until I started adding the add function. Any ideas?

CodePudding user response:

Your "add" function is on the main "if" statement, meaning that "Add" will only be checked for if email_numb == M (and all other conditions) is false.

You'll want to move it inside the if email_numb == "M" check:

    ...
    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":
                new_email = []
                name = input("Please type the name: ")
                new_email.append(name)
                email = input("Please type the email: ")
                new_email.append(email)
                print(new_email)
                new_pair = ":".join(new_email)
                emails.update({new_pair})
                continue

   elif email_numb != {'Email', 'Phone', "M"}:
   ...
  • Related