【问题标题】:Infinite looping, and only executing part of the script if first part is true无限循环,如果第一部分为真,则仅执行部分脚本
【发布时间】:2015-01-29 22:31:41
【问题描述】:

我创建了一个无限循环来检查好友请求,如果有的话,它会接受它们,因此脚本会永远继续下去。

在此处查看代码:

while True:

    snaps = s.get_snaps()

    for sender in snaps:
        if sender['media_type'] == 3:
            s.add_friend(sender[u'sender'])


    friends = s.get_friends()

    names = []
    for friend in friends:
        if friend['type'] == 0:
            names.append(friend[u'name'])

    print "Amount of confirmed friends:",
    print len(names),
    print "/ 6000"

到目前为止,该脚本有效,但不是我想要的。

它一遍又一遍地打印 X / 6000,这就是困扰我的地方。我只希望脚本在实际接受一个/多个好友请求时执行“打印”部分

所以这部分:

print "Amount of confirmed friends:",
print len(names),
print "/ 6000"

只有在某人被实际接受为好友时才能打印,从而更新已确认好友的数量。

我的心情很复杂,我在使用if语句等方面从来没有这么详细过。

我考虑过的场景是:

在它添加某人的地方放置一个 if 语句,如果结果是(好?)它继续到脚本的其余部分。

或者在它打印之前在底部放置一个 if 语句,说如果有人被实际添加,它就会打印,否则它会回到重复本身。

这里有几点需要注意,对 snapchat 服务器的请求/活动越少越好。因此,如果我可以在脚本的开头限制它,那将是首选。

虽然我不知道该怎么做。

谁能解释我如何检查是否有人(或多人)找到了 media_type 3,如果有,它会继续脚本。如果没有,它会休眠几秒钟,然后重新尝试。

我觉得这是一个微妙的话题,我担心我会弄乱我的代码,最终不知道我到底在做什么,并为此感到沮丧。在询问之前,我已经联系了 Google 和 TutorialsPoint,但我仍然无法应用我读过的任何东西,因为我不知道怎么做。

【问题讨论】:

  • 我不明白你想在这里做什么。您首先检查快照的媒体类型,将快照发件人添加为朋友,然后检查他是否是朋友?但是你说 loop that checks for friend requests, if there are any, it accepts them 与你的代码相矛盾。
  • 我正在尝试做的(或正在做的)如下:您只能通过 s.get_snaps() 看到好友请求,然后我过滤结果以便只显示类型 3 , 这些是好友请求。我过滤了名字,加上s.add_friend(sender)sender的名字就是这里过滤的名字。之后我想检查一下,我还可以添加多少人,因为限制大约是 6000(实际上有点多)所以我做了一个 s.get_friends() 并使用类型 0(确认的朋友)过滤结果,我做使用 len 进行计数,并打印添加的人数与最大人数。
  • 为什么不把代码的整个底部放在“if sender == 3”下面呢?这样它只会在您添加朋友后运行。
  • 不,它用 5 / 6000 向我的命令行发送垃圾邮件,它会继续运行
  • nvm 我扔这个,太费劲了。 Snapchat 只是有一个糟糕的 api。现在突然之间,我的其他帐户又开始显示为好友请求。所以我必须通过检查它们是否已经被确认等等来过滤它们。

标签: python if-statement while-loop


【解决方案1】:

为了尽量减少对 snapchat 的调用次数,我会考虑到 s.get_friends() 调用,所以它只在脚本运行时执行:

import time

total_friends = len([friend for friend in s.get_friends() 
                     if friend['type'] == 0])

SLEEP_TIME = 60

while True:
    new_friends = 0
    snaps = s.get_snaps()
    for sender in snaps:
        if sender['media_type'] == 3:
            s.add_friend(sender[u'sender'])
            new_friends += 1

    total_friends += new_friends

    if new_friends:
        print 'Amount of confirmed friends:'
        print total_friends, '/ 6000'
    time.sleep(60)

这将获取您在进入循环之前的当前好友数量。然后它得到(可能是新的?)快照,添加正确的media_type 并同时增加new_friends。如果new_friends 不为0,则打印新的好友总数。

【讨论】:

  • 好主意,但是这样会列出所有朋友,可能只列出类型0的朋友(已确认的朋友)
  • 哎呀。我打算在total_friends 中解决这个问题。在这种情况下,您只需执行len([friend for friend in s.get_friends() if friend['type'] == 0]) 快速列表理解以查看s.get_friends() 中的朋友,如果他们的'type' 为0,则将它们添加到列表中。将整个shebang 分配给total_friends,这应该模仿什么你有。我将编辑我的答案。
  • NameError: name 'new_friends' 未定义
  • 哎呀抱歉,忘记了一行,虽然脚本不工作。它不断总结数字,10 秒后它告诉我它有 47 个确认的朋友,而只有 5 个
  • get_snapsadd_friend 对我来说完全不透明。您提供的代码仅与检索数据有关,根据收到的数据做出一些决定,然后在适用时发布公告。 get_snaps 是否只检索上次调用 get_snaps 时收到的信息?我不知道。 add_friend 是否清除了一些队列?不知道。至于只是不停地跑来跑去的脚本,您可以添加暂停。使用import time,然后使用time.sleep(n),其中n 是您希望脚本在继续之前等待的秒数。
【解决方案2】:

我相信这个解决方案会对您有所帮助。您会记录第一次收到朋友的信息,以避免在第一次运行时发送消息。然后您只需将 previous_friends 与当前好友进行比较即可指示更改。

# Used to mark the initial receiving of the friends
friends_received = False
# Will store the names of all friends from the last loop
previous_friends = []

while True:
    new_friend = False
    snaps = s.get_snaps()

    for sender in snaps:
        if sender['media_type'] == 3:
            s.add_friend(sender[u'sender'])


    friends = s.get_friends()

    names = []
    for friend in friends:
        if friend['type'] == 0:
            names.append(friend[u'name'])

    # Checking if new friends have been received
    if friends_received and len(friends) > len(previous_friends):
        # New friends are available
        new_friend = True

    if new_friend:
        print "Amount of confirmed friends:",
        print len(names),
        print "/ 6000"

    # Keeping a record of the friends from the last loop
    if len(previous_friends) == 0:
        friends_received = True
    previous_friends = friends

【讨论】:

    【解决方案3】:

    这样做怎么样:

    friendAdded = false
    snaps = s.get_snaps()
    
    for sender in snaps:
        if sender['media_type'] == 3:
            s.add_friend(sender[u'sender'])
    
    
    friends = s.get_friends()
    
    names = [];
    for friend in friends:
        if friend['type'] == 1:
            names.append(friend[u'name'])
            friendAdded = true
    
    
    if friendAdded == true:
        print "Amount of confirmed friends:",
        print len(names),
        print "/ 6000"
        friendAdded = false
    

    现在它只会在您添加新朋友时打印。

    【讨论】:

    • 这行不通,因为脚本的后半部分只是查找已确认的好友,这些好友将一直存在,它查找好友只是为了获取要打印的数字,因此 print len(names),
    • 唯一可行的方法是向其添加一个 IF 语句,仅在再次接受好友或多个好友请求时才执行,这使我们回到了原始帖子。感谢您与我一起看!
    • @Snowlav 根据您的要求更新了答案...我认为这就是您想要的。
    • 你的某些行末尾的 ; 是什么?
    • @Maxime 哎呀!整天都在C# 编码:P
    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2011-12-18
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多