gpt4 book ai didi

python - 我可以在程序中多次调用函数 'main' 吗?

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:15 29 4
gpt4 key购买 nike

我正在编写一个程序,让用户在命令行 GUI 中输入选择,并且不希望程序在用户选择“退出”之前结束。

到目前为止,我唯一的解决方案是通过调用“main()”来结束主力函数,以免退出。

def parse(file):
out_file = functions(file)
main()

def get_selection():
return selection

def main():
file_to_parse = get_selection()
parse(file_to_parse)
return

if __name__ == '__main__':
main()

我的代码可以运行,目前不需要考虑时间或安全性方面的其他考虑。我只是想知道我犯的编程错误/错误/坏意见有多么严重。

最佳答案

def parse(file):
out_file = functions(file)

def get_selection():
return selection

def main():
file_to_parse = get_selection()
while file_to_parse.lower() != 'exit':
parse(file_to_parse)
file_to_parse = get_selection()
return

if __name__ == '__main__':
main()

也许这样的事情会起作用。查看 Python 中的循环和字符串。

关于python - 我可以在程序中多次调用函数 'main' 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57876882/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com