Python If语句被忽略
当我使用以下循环运行pythontweepy函数时,下面的if语句将被忽略。返回的结果是“高”,即使他们应该说“非常高”或“中等”。似乎y总是显示“假”,即使有时它应该是“真”,从而将状态从“高”更改。同样,标志始终显示为0Python If语句被忽略,python,api,tweepy,Python,Api,Tweepy,当我使用以下循环运行pythontweepy函数时,下面的if语句将被忽略。返回的结果是“高”,即使他们应该说“非常高”或“中等”。似乎y总是显示“假”,即使有时它应该是“真”,从而将状态从“高”更改。同样,标志始终显示为0 for i in xrange(0, len(ids), segment): for follower in client.lookup_users(user_ids=ids[i:i+segment]): flags=0 x='fa
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
的值,因此当您到达if
块时,它应该始终是'false'
。您的意思是在上面的一个点中设置它而不是x
?对于像“x”和“y”这样的名称,很难猜出它们是什么意思,但您永远不会更新y
,因此您不应该期望其值发生变化
另外,为什么不使用
True
和False
而不是包含这些内容的字符串作为单词?您可能应该使用python提供的布尔类型,而不是使用字符串。y
在代码中总是'false'
。您在开始时设置了y='false'
,并且从未将y
设置为任何不同的值。此外,为什么要使用字符串值'true'
和'false'
而不是布尔值true
和false
?为什么还要使用整数作为字符串?Friendsum==2001。最好是我添加了更多的代码来澄清这个问题。我应该补充一点,即使不改变y,if语句也不起作用。例如,尽管有if语句,Flags始终返回0。我有理由使用true和false作为字符串。这些不是实际的布尔语句,它们是稍后可能要记录到csv文件中的变量。如果您没有向我们显示相关代码,您希望我们如何调试这些变量?你是在要求我们对布尔值(顺便说一句,应该是布尔值,而不是字符串)进行推理,而不透露它们的设置。想一想,如果语句被“忽略”,您如何知道?你有什么证据表明标志
首先应该是积极的?还是说朋友的支持率足够高?或者说lowFollowHighFriends
(听起来确实是一样的)是真的?你们提供了哪些投入?你得到了什么结果?为了清楚起见,我编辑了这个问题。不仅仅是Y,if语句中的任何内容都会被忽略,例如,包括标志。@auto-Well,我建议您尝试调试或在关键位置添加print
语句,以查看变量中实际包含的值。我向您保证,if
语句不会被忽略,但您可能没有执行预期的测试,或者您可能没有预期的值。你使用了很多我们看不到的变量,所以你可以自己决定这些变量可能包含什么