首页 > 解决方案 > 如何重新启动包含在函数中的代码?

问题描述

我想在用户输入给定的“是”后从头开始重新启动我的 python 代码,但我不明白我在这里做错了什么:

if input("Other questions? ") == 'yes' or 'yeah':
    main()
else:
    pass

我的代码包含在函数 main() 中。谢谢您的帮助!!

标签: pythonpython-3.x

解决方案


你可能会在你想要重复的整个事情上使用一个 while 循环来做到这一点,正如 Loic RW 在评论中所说,只需跳出 while 循环:

while True:
    # do whatever you want

    # at the end, you ask your question:
    if input("Other questions? ") in ['yes','yeah']:
        # this will loop around again
        pass
    else:
        # this will break out of the while loop
        break

推荐阅读