【问题标题】:Recalling a function iteratively迭代地调用函数
【发布时间】:2022-01-05 23:19:12
【问题描述】:

我有一个关于在循环中调用我的函数的问题。

下面是我的代码:

List_new = myfunction()
for items in List_new:
    if(my condition is TRUE):
        Execute some commands
    if(my condition is FALSE):
        recall myfunction()

我的问题是我正在使用 myfunction() 加载“List_new”。当我的条件为 False 时,如何迭代地更改“List_new”。我想重新加载函数,尤其是当条件为 FALSE 时。这意味着我会一直调用该函数,直到它为 false,然后执行 myfunction() 的最终输出。

提前感谢您的帮助。

【问题讨论】:

  • 很难理解,myfunction()是在哪里定义的?我的条件在 python 中是不允许的,因为它有空格,你需要发布完整的代码。
  • @rydex 您要查找的术语是minimal reproducible example
  • 问题是关于一般的逻辑方向。疏忽。

标签: python function loops iteration logic


【解决方案1】:

我将假设myfunction() 生成的列表是一个固定大小。

你可以使用while循环:

List_new = myfunction()
index=0
while index < len(List_new):
    
    items = List_new[index]
    if(my condition is TRUE):
        #Execute some commands
        i+=1
    if(my condition is FALSE):
        List_new = myfunction()

请记住,如果myfunction() 不断生成错误值,此解决方案将出现无限循环。如果您可以保证所有值最终都是True,那么它应该总是终止。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 2021-02-07
    相关资源
    最近更新 更多