Reputation: 15
I have the following code which is suppose to search for the letter 'e' within a string, return False
if 'e' was found within the string and return True
is the 'e' was not found :
def has_no_e(word):
for letter in word:
if letter == 'e':
return False
return True
As per my understanding, the for
loop will access the characters within word
one by one and check them against the condition i've set, which is if letter == 'e'
Each time i run the program, it seems the for loop is only checking the first letter for some reason!
so calling the function with has_no_e('dde','e')
will result return True
. but if i switch it around has_no_e('edd','e')
it will return False
I'm running Python 2.7.3 on a x86 Windows 7 Ultimate machine... Thanks
Upvotes: 0
Views: 83
Reputation: 599450
Others have pointed out your error, but the whole function is overly complicated. You can just do this:
def has_no_e(word):
return 'e' not in word
Upvotes: 7
Reputation: 19766
your return
is indented wrong. unindent it (to the same level as for
), and it'll work
Upvotes: 3
Reputation: 133504
return True
is inside the body of the for loop so it will execute straight away after it checks if the first letter is 'e'
(unless the first letter is e
then it will return False
).
Upvotes: 3