【问题标题】:Python If statements are being ignoredPython If 语句被忽略
【发布时间】:2015-08-13 00:43:34
【问题描述】:

当我使用以下循环运行 python tweepy 函数时,下面的 if 语句将被忽略。返回的结果是“高”,即使它们应该说“非常高”或“中等”。似乎 y 总是出现“假”,即使它有时应该是“真”,从而从“高”改变状态。同样,flags 总是显示为 0。

for i in xrange(0, len(ids), segment):

    for follower in client.lookup_users(user_ids=ids[i:i+segment]):
        flags=0
        x='false'
        y='false'
        status=''
        values=[]
        userId = userId
        name =str(follower.name.encode('utf-8'))
        screen_name = str(follower.screen_name.encode('utf-8'))
        background =str(follower.default_profile)
        profilePic = str(follower.default_profile_image)



        #Check if friends to follow ratio is higher than limit set above
        if friendsFollowersRatio>highfriendsFollowRatio:
            highFollow = 'true'
            x = 'true'
        else:
            highFollow = 'false'

        if lowFollowHighFriends == 'true':
            x = 'true'

        if tweetNum >= xNumberOfTweets:
            x = 'true'


        if background=='true' and profilePic=='true':
            flags = flags+2
        if screen_name==name:
             flags= flags+1
        if flags>1:
            y='true'


        if x=='true'and y=='true':
            status = 'very high'
        elif x=='true'and y=='false':
            status='high'
        elif x=='false'and y=='true':
            status='medium'
        #ADD INACTIVE
        else:
            status=' blank'



        #Put stuff here
        userId = userId + 1

        #add values to array
        values.append(userId)
        values.append(name)
        values.append(screen_name)
        values.append(background)
        values.append(profilePic)

 ####Create csv file ######
        #Add values as a new row in the data
        csvFile = open('file.csv','ab')
        #prep file to be written on
        newFile =csv.writer(csvFile)


        newFile.writerow(values)

        #close file
        csvFile.close()

【问题讨论】:

  • y is 在你的代码中总是'false'。您在开头设置了y = 'false',并且从未将y 设置为任何不同的值。
  • 另外,你为什么使用 string'true''false' 而不是布尔值 TrueFalse
  • 另外你为什么使用整数作为字符串? FriendsNum == 2001。会更好
  • 我添加了更多代码来澄清问题。我应该补充一点,即使没有更改 y,if 语句也不起作用。例如,标志总是返回 0,尽管有任何 if 语句。我有理由使用 true 和 false 作为字符串。这些不是实际的布尔语句,它们是以后可能想要记录到 csv 文件中的变量
  • 当您没有向我们展示相关代码时,您希望我们如何调试它?您要求我们对布尔值进行推理(顺便说一下,它应该布尔值,而不是字符串)而没有透露它们的设置。想一想,怎么知道if语句被“忽略”了?你有什么证据表明flags 首先应该是积极的?还是friendsFollowersRatio 足够高?或者lowFollowHighFriends(听起来确实是同一件事)是真的?你提供什么输入?你得到了什么输出?

标签: python api tweepy


【解决方案1】:

我在任何地方都没有看到您更新了y 的值,因此当您到达if 块时,它应该始终是'false'。您的意思是在上面的某个位置设置它而不是 x 吗?对于像“x”和“y”这样的名称,很难猜出你的意思,但你永远不会更新y,所以你不应该期望它的值会改变。

另外,你为什么不使用TrueFalse 而不是包含它们作为单词的字符串?您或许应该使用 python 提供的布尔类型,而不是使用字符串。

【讨论】:

  • 为了清楚起见,我编辑了这个问题。不仅仅是 Y,if 语句中的任何内容都会被忽略,例如标志。
  • @auto 好吧,我建议要么尝试调试器,要么在关键位置添加 print 语句,以查看变量中实际具有的值。我向您保证if 语句不会被忽略,但您可能没有进行您期望的测试,或者您可能没有您期望的值。你使用了很多我们看不到被赋值的变量,所以你对那些可能持有的东西有点靠自己
猜你喜欢
  • 2018-03-26
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多