在Python中循环浏览时遇到麻烦

时间:2019-04-02 05:08:00

标签: python python-3.x

我已经问过我周围的人,并无数次地尝试获取此修复程序。该程序应该能够根据需要多次添加用户的网站和密码,并显示他们选择的网站和密码。

现在,当您以回答import numpy as np autoenc_model.fit(np.ones((5,128,128,3)), np.ones((5,128,128,3))) dec_model.predict(np.ones((1,8,8,2))) 时,它并不需要输入新的网站名称和密码,而只是重复问题--no-banner,即使您有输入网站名称和密码,并用回答would you like to add another website?,然后选择选项1以查看现有帐户,然后重复would you like to add another website?,甚至在选项1处出现< / p>

输入及其应如何输出:

1)查找现有密码
2)为您的应用保存新密码
3)查看密码柜的摘要
4)成功退出密码柜 1

您没有存储的网站和密码

1)查找现有密码
2)为您的应用保存新密码
3)查看密码柜的摘要
4)成功退出密码柜 2

您要添加的网站/应用的名称是什么? instagram

您的{instagram}帐户的密码是什么? bob91

您要添加其他网站吗?

您要添加的网站/应用的名称是什么? facebook

您的{facebook}帐户的密码是什么? bob92

您要添加其他网站吗?

1)查找现有密码
2)为您的应用保存新密码
3)查看密码柜的摘要
4)成功退出密码柜 1

输入您要为 instagram
查找密码的应用 网站名称= instagram
密码= bob91

完整代码:

would you like to add another website?

2 个答案:

答案 0 :(得分:1)

您的代码不起作用,因为您没有从break循环中while True:进入

while True: 
    ask_again = input('''Would you like to add another app and password?''')
    if ask_again.lower() == "yes":
        locker_menu_var = "2"            <--- does not leave while loop
    elif ask_again.lower() == "no":
        locker_menu_func()
    else:
        # etc.

将您的方法缩小并处理一个问题以简化控制流程,例如:

vault_apps = {}
# ,2,Hallo,Yuhu,y,Hallo2,Yuh,n,3,4
def menu():
    print('\n'+'-'*40)
    print('1) find your existing passwords')
    print('2) save a new password for your apps')
    print('3) see a summary of your password locker')
    print('4) exit password locker successfully')
    print('-'*40)
    k = None
    while k not in {"1","2","3","4"}:
        k = input("Choose: ")

    return int(k) # return the number chosen 

def input_new_app():
    global vault_apps
    app = None
    while not app:
        app = input("What is your apps name? ")
    pw = None
    while not pw:
        pw = input("What is your apps passphrase? ")
    vault_apps[app]=pw

def print_vault():
    print("Vault content:")
    for key,value in vault_apps.items():
        print(f"  {key:<10}\t==>\t{value}")

def find_password():
    if vault_apps:
        pass
    else:
        print("nothing in your password store")

def main():
    k = None    
    print('You have opened the locker,\nPlease select what you would like to do.')
    while True:  
        choice = menu()
        if choice == 1:
            find_password()
        elif choice == 2:
            input_new_app()
            k = input("Would you like to add another app and password?").lower()
            while  k in {"yes","y"}:
                input_new_app()
        elif choice == 3:
            print_vault()
        elif choice == 4:
            print("Good bye")
            break

main()      

输出:

You have opened the locker,
Please select what you would like to do.

----------------------------------------
1) find your existing passwords
2) save a new password for your apps
3) see a summary of your password locker
4) exit password locker successfully
----------------------------------------
Choose: 1
nothing in your password store

----------------------------------------
1) find your existing passwords
2) save a new password for your apps
3) see a summary of your password locker
4) exit password locker successfully
----------------------------------------
Choose: 2
What is your apps name? A
What is your apps passphrase? 66
Would you like to add another app and password? n
----------------------------------------
1) find your existing passwords
2) save a new password for your apps
3) see a summary of your password locker
4) exit password locker successfully
----------------------------------------
Choose: 3 
Vault content:
  A             ==> 66

----------------------------------------
1) find your existing passwords
2) save a new password for your apps
3) see a summary of your password locker
4) exit password locker successfully
----------------------------------------
Choose: 4
Good bye

答案 1 :(得分:0)

这种逻辑是有缺陷的。 一旦进入无限while循环,就只能输入“ no”来退出它。输入“是”时,locker_menu_var的值会更改,但不会退出循环,因此它将继续重复相同的菜单。

            while True:     
            ask_again = input('''Would you like to add another app and password? 
        ''')
            if ask_again.lower() == "yes":
                locker_menu_var = "2"
            elif ask_again.lower() == "no":
                locker_menu_func()

您正在混合循环和递归,这使事情变得混乱。一种简单的方法是:

vault_apps = []

def locker_menu():

    # the entry message
    msg = '''You have opened the locker, Please select what you would like to do,'''
    print(msg, end="\n\n")

    # nume options
    menu = ["1) find your existing passwords", 
    "2) save a new password for your apps", 
    "3) see a summary of your password locker", 
    "4) exit password locker successfully"]
    menu_string = f"Press:\n{menu[0]}\n{menu[1]}\n{menu[2]}\n{menu[3]}\n"

    # now enter the loop
    while True:

        # input variable
        # NOTE: This variable is inside the loop, 
        # so that the user can enter the value everytime
        # the loop repeates.
        locker_menu_var = input(menu_string)

        if locker_menu_var == "1":
            # retrieve password for an existing record.
            # although it's not a good idea to just print
            # all the records, I am not changing your application logic
            # because I don't see any use in it.

            # you missed one case in your logic,
            # which I have fixed here.
            if len(vault_apps) == 0:
                print("you have nothing stored")
            else:
                print(vault_apps)

        elif locker_menu_var == "2":
            # for a new entry
            # enter your logic here
            an = input("app name: ")
            passw = input("password: ")
            vault_apps.append([an, passw])
            done = False # flag for exiting
            while not done:
                inp = input("enter another?")
                if inp == "yes":
                    # enter logic here
                    an = input("app name: ")
                    passw = input("password: ")
                    vault_apps.append([an, passw])
                else:
                    done = True
        elif locker_menu_var == "3":
            # do something
            pass
        elif locker_menu_var == "4":
            return          

if __name__ == "__main__":
    locker_menu()