How can I transform my for loop code into the while loop in Python?

I am trying to write a function named has_no_e that takes a string as an argument and returns False if the string contains the letter e and True if the string does not contains the letter e.

I have written the function using the for loop but I am failing to write a similar function that does the same thing using a while loop.

Here is my function with the for loop:

def has_no_e(word):
    for letters in word:
        if letters == "e":
            return False
    return True

And the function I have written is:

def has_no_e2(word):
    index = 0
    while index < len(word):
        if word[index] != "e":
            index += 1
        return False
    return True

Can you tell me what is wrong with my function with the while loop? It returns False every time not depending on whether there is "e" or not.

Upvotes: 0

Views: 62

Answers (6)

JenilDave
JenilDave

Reputation: 604

Some changes and you are good to go, you just need to write whatever condition you wrote in for loop into while, that's it:

def has_no_e2(word):
    index = 0
    while index < len(word):
        if word[index] == "e":
            return False
        index+=1
    return True  

Now let me give you a one liner:

>>> string = 'hello'
>>> False if string.count('e') > 0 else True
False
#changing the contents
>>> string = 'hlo'
>>> False if string.count('e') > 0 else True
True

Upvotes: 0

Maurice Meyer
Maurice Meyer

Reputation: 18106

You can use the in operator:

def has_no_e(word):
    return 'e' not in word

In case you really need a while loop:

def while_has_no_e(word):
    characters = list(word)
    while characters:
        if characters.pop() == 'e':
            return False
    return True


print(has_no_e('foo'))
print(has_no_e('element'))

print(while_has_no_e('foo'))
print(while_has_no_e('element'))

Out:

True
False
True
False

Upvotes: 0

Alon Barad
Alon Barad

Reputation: 1971

def has_no_e1(_string):
    return 'e' not in _string


def has_no_e2(_string):
    return _string.find('e') == -1


def has_no_e3(_string):
    while _string:
        if _string[0] == 'e':
            return False
        _string = _string[1:]
    return True


if __name__ == "__main__":
    word = 'With the letter `e`'
    print(
        has_no_e1(word),
        has_no_e2(word),
        has_no_e3(word),
    )
    word = 'Without it'
    print(
        has_no_e1(word),
        has_no_e2(word),
        has_no_e3(word),
    )

Upvotes: 0

Cadell
Cadell

Reputation: 61

Try:

    index = 0
    while index < len(word):
        if word[index] != "e":
            index += 1
        else:
            return False
    return True

Currently your code returns false at the end of the first loop

Upvotes: 0

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

There are two ways to implement the same fix for this. The one that keeps more closely to your existing code is

def has_no_e2(word):
    index = 0
    while index < len(word):
        if word[index] != "e":
            index += 1
            continue  # skip the rest of the loop and go back to the top
        return False
    return True

The better one is

def has_no_e2(word):
    index = 0
    while index < len(word):
        if word[index] == "e":
            return False
        index += 1
    return True

Note that the following two pieces of code are roughly equivalent:

for elem in iterable:
    # code

_index = 0
while _index < len(iterable):
    elem = iterable[_index]
    # code
    _index += 1

It's more complicated than that, because for loops often use iterators instead of indices, but you get the idea.

Upvotes: 2

Mighty Diffy
Mighty Diffy

Reputation: 98

You don't need to create a function, it exists already and it's called find()

has_e = word.find("e")

The function either returns the exact position of the letter or it returns -1 if it doesn't exist.

Reference

Upvotes: -1

Related Questions