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