Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中适当地实现无限while循环?_Python_Python 3.x_While Loop - Fatal编程技术网

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

如何在Python中适当地实现无限while循环?,python,python-3.x,while-loop,Python,Python 3.x,While Loop,我正在创建一个程序,允许用户使用1、2和3键登录、创建或恢复他们的帐户 mainf模块要求用户选择一个选项,然后从另一个模块调用一个函数 mainf模块: import createnewaccount import loginf import restoreaccount def main(): options = { "LOGIN": 1, "CREATE NEW ACCOUNT": 2, "RESTORE ACCOUNT": 3,

我正在创建一个程序,允许用户使用
1
2
3
登录
创建
恢复
他们的帐户

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()
def login_information():
    return print("Hello")
用户每次选择键
1
时都会收到
“Hello”
。 但是,这是我的输出:

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: 

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

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\u 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()
导入createnewaccount
导入登录
导入恢复计数
def get_选项():
选项={
“登录”:1,
“创建新帐户”:2,
“恢复帐户”:3,
}
打印(-----------------------------------)
对于选项,请选择options.items()中的选项:
打印(选项+'-'+str(选项))
打印(-----------------------------------)
尽管如此:
尝试:
用户\选项=(输入(“选择选项1-3:\n”))
用户选项=int(用户选项)
如果用户选项<1或用户选项>3:
引发异常
除值错误外:
打印(“无效响应”)
除例外情况外:
打印(“无效选项”)
其他:
返回用户\u选项
def main():
用户=获取选项()
如果用户=1:
loginf.login_信息()
如果用户=2:
createnewaccount.main()
如果用户=3:
restoreaccount.main()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
尽管如此:
main()
这是因为“如果条件”不在无限循环中,它们在文件执行时只执行一次

通过将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()

我认为你选择的题目并没有完全理解你正在努力解决的问题。“如何在Python中适当地实现无限while循环?”,简单地说,while True:第二个循环中有一个错误。您正在循环使用
main()
,但这只是您第一次调用
loginf.login\u information()
。因此,总是有一个
1
返回,但只有一个
Hello
返回。