-1

Hi I have been looking for an answer to this question for my lab and I can't seem to find it on here. If it's been asked already I apologize. With that said I am making a password vault menu and I am stuck on the very last section (the else statement at the bottom) of the lab. If the user gives an unknown input I am supposed to print what is in my attached code and he specifies to continue the loop. The clear solution to the infinite loop is a break statement but that makes additional inputs not work so if anyone could help me allow the user to provide an input without copying all of that again for the else I would greatly appreciate it.

else:
    print("   ======Menu======") 
    print("1. View Password Vault")
    print("2. Add a new credential")     
    print("3. Update an existing credential")
    print("4. Exit")
menu_item = input("\nChoose a menu item: ")
menu_item = True
while menu_item == True:
    if menu_item == "1":
        print(password_vault)
    elif menu_item == "2":
        website_name = input("What site: ")
        email_address = input("What email: ")
        password = input("What password: ")
        validate(email_address, password)
        password_vault[website_name.title()] = {email_address:password}
    elif menu_item == "3":
        website_name = input("What site: ")
        email_address = input("What email: ")
        password = input("What password: ")
        if website_name in password_vault:
            validate(email_address, password)
            password_vault[website_name.title()] = {email_address:password}
            print(password_vault)
    if menu_item == "4": 
        menu_item = False
    else:
        print("Unknown value entered. Please try again!")
        break
  • You ask for a menu item, and then you immediately overwrite that value with `True`. Use a different name. Even more, however, the `input` line should be INSIDE the loop. The loop should be `while True:`, your `menu_item = False` should just be `break`, and you should delete the final `break`. – Tim Roberts Oct 16 '22 at 07:14

1 Answers1

0

This is what you are looking for.

while True:
    print("   ======Menu======") 
    print("1. View Password Vault")
    print("2. Add a new credential")     
    print("3. Update an existing credential")
    print("4. Exit")

    menu_item = input("\nChoose a menu item: ")
    if menu_item == "1":
        print(password_vault)
    elif menu_item == "2":
        website_name = input("What site: ")
        email_address = input("What email: ")
        password = input("What password: ")
        validate(email_address, password)
        password_vault[website_name.title()] = {email_address:password}
    elif menu_item == "3":
        website_name = input("What site: ")
        email_address = input("What email: ")
        password = input("What password: ")
        if website_name in password_vault:
            validate(email_address, password)
            password_vault[website_name.title()] = {email_address:password}
            print(password_vault)
    elif menu_item == "4": 
        break
    else:
        print("Unknown value entered. Please try again!")
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30