Reputation: 1
This is my code. It doesn't print ''No matches found'' I think that it has to do with the section start program
scores_and_similarities = "somestring" # value can be empty
similarities = scores_and_similarities.split(',')
if similarities == '':
print('\tNo matches found')
for similarity in similarities:
print('\t%s' % similarity)
Upvotes: 0
Views: 150
Reputation: 140178
str.split
returns a list
not a string
. Test your value using truthiness instead:
similarities = scores_and_similarities.split(',')
if not similarities # better than if similarities == []
print('\tNo matches found')
note that str.split
returns an empty list just when the input string is empty. So you could test
if not scores_and_similarities:
print('\tNo matches found')
else:
# split and process
although I suspect that you're expecting str.split
to return empty list if string doesn't contain a comma but it's not:
>>> ''.split(",")
>>> []
>>> 'iii'.split(",")
['iii']
so maybe you want to test if ,
is in the string (note: testing if splitted string has 1-length does the same:
if ',' not in scores_and_similarities:
print('\tNo matches found')
Upvotes: 2
Reputation: 4664
This is because after the split it returns empty list []
not an empty string ''
.
scores_and_similarities = "somestring" # value can be empty
similarities = scores_and_similarities.split(',') # empty list is returned
if similarities == '': # [] is not equal to ''
print('\tNo matches found')
for similarity in similarities:
print('\t%s' % similarity) # so answer is this
Upvotes: 0