如何在Python中适当实现无限while循环?

问题描述 投票:0回答:2

我正在创建一个程序,该程序允许用户使用键logincreaterestore123他们的帐户。

mainf模块要求用户选择一个选项,然后从另一个模块中调用一个函数。

mainf模块:

import createnewaccount
import loginf
import restoreaccount


def main():
    options = {
        "LOGIN": 1,
        "CREATE NEW ACCOUNT": 2,
        "RESTORE ACCOUNT": 3,
    }

    print("------------------------")
    for options, choices in options.items():
        print(options + ' - ' + str(choices))
    print("------------------------")

    while True:
        try:
            user_option = (input("Select an option 1-3: \n"))
            user_option = int(user_option)
            if user_option < 1 or user_option > 3:
                raise Exception
        except ValueError:
            print("Invalid response.")
        except Exception:
            print("Invalid option.")
        else:
            return user_option


user_ = main()

if user_ == 1:
    loginf.login_information()
if user_ == 2:
    createnewaccount.main()
if user_ == 3:
    restoreaccount.main()

if __name__ == '__main__':
    while True:
        main()

为了调试我的代码,我已经删除了loginf模块中的所有内容,而只允许其为return "Hello"

loginf模块:

def login_information():
    return print("Hello")

用户每次选择键"Hello"时都会收到1。但是,这是我的输出:

C:\Users\raamis\PycharmProjects\test\venv\Scripts\python.exe C:/Users/raamis/PycharmProjects/test/mainf.py
------------------------
LOGIN - 1
CREATE NEW ACCOUNT - 2
RESTORE ACCOUNT - 3
------------------------
Select an option 1-3: 
1
Hello
------------------------
LOGIN - 1
CREATE NEW ACCOUNT - 2
RESTORE ACCOUNT - 3
------------------------
Select an option 1-3: 
1
------------------------
LOGIN - 1
CREATE NEW ACCOUNT - 2
RESTORE ACCOUNT - 3
------------------------
Select an option 1-3: 
python python-3.x while-loop
2个回答
0
投票

当您通过命令行运行模块时,将首先执行以下指令:

user_ = main()

if user_ == 1:
    loginf.login_information()
if user_ == 2:
    createnewaccount.main()
if user_ == 3:
    restoreaccount.main()

这说明“ Hello”是第一次打印(按预期方式)。

然后,执行以下指令:

while True:
    main()

这说明下次不会打印“ Hello”,因为此处未调用loginf.login_information()

我建议您进行以下修复:

import createnewaccount
import loginf
import restoreaccount


def get_option():
    options = {
        "LOGIN": 1,
        "CREATE NEW ACCOUNT": 2,
        "RESTORE ACCOUNT": 3,
    }

    print("------------------------")
    for options, choices in options.items():
        print(options + ' - ' + str(choices))
    print("------------------------")

    while True:
        try:
            user_option = (input("Select an option 1-3: \n"))
            user_option = int(user_option)
            if user_option < 1 or user_option > 3:
                raise Exception
        except ValueError:
            print("Invalid response.")
        except Exception:
            print("Invalid option.")
        else:
            return user_option

def main():
    user_ = get_option()

    if user_ == 1:
        loginf.login_information()
    if user_ == 2:
        createnewaccount.main()
    if user_ == 3:
        restoreaccount.main()

if __name__ == '__main__':
    while True:
        main()

0
投票

这是因为“如果条件”不在无限循环中,则在文件执行时它们仅执行一次。

您可以通过将if条件置于如下所示的无限while循环中来达到预期的结果-

if __name__ == '__main__':
    while True:
        user_ = main()

        if user_ == 1:
            loginf.login_information()
            print("inside ")
        if user_ == 2:
            createnewaccount.main()
        if user_ == 3:
            restoreaccount.main()
热门问题
推荐问题
最新问题