CodingIsPain
CodingIsPain

Reputation: 1

How to convert this for loop into a while loop (Python)?

I'm currently attempting to find a way to change this for loop into a while loop, but i'm completely lost. i'm new to this whole python thing, so help would be really nice!

def ReturnConsonants(string):
    newStr=""
    vowels="aeiouAEIOU"
    for i in string:
        if not(i in vowels):
            newStr+=i
    return(newStr)
string=input("enter word: ")
print(ReturnConsonants(string))

Upvotes: 0

Views: 61

Answers (2)

Here is your code using while loop:

def ReturnConsonants(string):
    newStr=""
    vowels="aeiouAEIOU"
    i=0
    while i < len(string):
        if not(string[i] in vowels):
            newStr+=string[i]
        i=i+1
        return(newStr)
string=input("enter word: ")
print(ReturnConsonants(string))


You can do this so many way. Good Luck.

Upvotes: 0

ForceBru
ForceBru

Reputation: 44838

For example:

j = 0
while j < len(string):
    i = string[j]
    if not (i in vowels):
        newStr += i
    j += 1

Upvotes: 1

Related Questions