【问题标题】:Counting the number of times an action have been performed计算一个动作被执行的次数
【发布时间】:2017-09-12 09:55:30
【问题描述】:

这是一个计算len(set_forbidden) > 0 次数的示例代码。现在,如果它计算的条件大于 3,那么我必须调用openthedoor(uppumanga),然后将计数重置为零。但是当我尝试打印 count 时,我得到 count = 1,任何解决此问题的帮助将不胜感激。

count = 0
def rydbergset(value,count):
if len(set_forbidden) > 0:
    count+=1
    last_time = True
    print "this is the count greater than three-"+ str(count)
    if (last_time == True) and (count>3):
        openthedoor(uppumanga)
        count = 0
    else:
        last_time = False
return set_forbidden,count

【问题讨论】:

  • 在 Python 中你需要 and 来表示逻辑和...& 表示按位和...
  • @JonClements 这是一个错误!现在计数也没有更新。所以它没有调用openthedoor(uppumanga)
  • 退后一步,再次查看您的代码...您有 last_time 和 last_time_(带下划线)...我们也不知道 set_forbidden 或 @ 的位置/内容987654329@ 来自...
  • 循环中可能有错误。您应该展开代码。
  • @JonClements 我只是展示了我询问的相关代码的一小部分。

标签: python function loops counter


【解决方案1】:

count == 0 0分配给count变量, count = 0 将完成这项工作

还请确保您将输出 count 分配给您的全局 count

(set_forbiden, count) = rydbergset(value, count)

【讨论】:

  • 我更新了代码,现在计数也没有更新!
  • @mudaliyar 请查看我的最新编辑。确保将输出计数分配给全局计数!
【解决方案2】:

如果我正确理解这个问题,我认为你需要这样的东西

count = 0
def rydbergset(set_forbidden, count):
    if len(set_forbidden) > 0:
        count+=1
        if (last_time == True) and (count>3):
            last_time = True  # Only relevant after you pass the check.
            print "this is the count greater than three-"+ str(count) # Only relevant after you pass the check.
            openthedoor(uppumanga)
            count = 0
        else:
            last_time = False
    return set_forbidden,count

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2014-10-06
    相关资源
    最近更新 更多