For 循环仅运行 1 次,而不是应有的 2 次

发布于 2025-01-10 02:59:30 字数 416 浏览 4 评论 0原文

我有一个包含两行的列表,第一行应该触发代码的“post.locked”部分,下一行应该触发“else”部分。

如果我运行这段代码,那么它只会打印出“if”部分,而不打印出“else”部分。为什么?

    for post in reddit.subreddit(content[nr].rstrip()).new(limit = 1):
        if post.locked:
            content[nr] = content[nr + 1]
            print(post.permalink)
        else:
            print(post.permalink)
            content[nr] = content[nr + 1]
            break

I have a list of 2 lines, first one should trigget the "post.locked" part of the code and the next line should trigger the "else" part.

If I run this code then it will only print out the "if" part but not the "else" part. Why?

    for post in reddit.subreddit(content[nr].rstrip()).new(limit = 1):
        if post.locked:
            content[nr] = content[nr + 1]
            print(post.permalink)
        else:
            print(post.permalink)
            content[nr] = content[nr + 1]
            break

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天暗了我发光 2025-01-17 02:59:30

好吧,我是个白痴 - 我不得不把整个事情放在另一个 for 循环中,如下所示:

    for line in content:
    for post in reddit.subreddit(content[nr].rstrip()).new(limit = 1):
        if post.locked:
            content[nr] = content[nr + 1]
            print("if" + post.permalink)
            print(content[nr])
        else:
            print("else" + post.permalink)
            content[nr] = content[nr + 1]

Ok, I was an idiot - I had to put the whole thing in another for loop like this:

    for line in content:
    for post in reddit.subreddit(content[nr].rstrip()).new(limit = 1):
        if post.locked:
            content[nr] = content[nr + 1]
            print("if" + post.permalink)
            print(content[nr])
        else:
            print("else" + post.permalink)
            content[nr] = content[nr + 1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文