While loop in Anti_vowel exercise


#1

Why does the terminal not accept my solution:

def anti_vowel(text):
word = “”
i = 0
while len(text) - i > 0:
if text[i] in “aeiouAEIOU”:
i += 1
else:
word = word + text[i]
i += 1
else:
return word

I know that it works, it is actually quite a bit more efficient. Am I meant to use a for loop for practice or something?

Thank you!

Strings and Console Output


#2

What does it say or show when not accepting your code, and what does your code actually look like? (need to run YOUR code, not something similar)


#3

Hey there,

thanks for getting back to me. I ended up leaving it for an hour or so, coming back, and when I came back and copied my code back into the window, it worked fine.

It wasn’t returning an error message, it was just loading endlessly, almost as if it was stuck in an infinite loop. I reset the page a few times and the same thing kept happening.

Not sure what changed, but everything seems fine now.

Here is the code if you still want to take a look

def anti_vowel(text):
  word = ""
  i = 0
  while len(text) - i > 0:
    if text[i] in "aeiouAEIOU":
      i += 1
    else:
      word = word + text[i]
      i += 1
  else:
      return word

#4

My first thought is obviously that if you copied it back from your forum post then you did edit it to get it to run. That might not be the case though, no point speculating.

I have a few jabs to take at that code. In particular because you say it’s more efficient (what is it doing faster?)

A for-loop would be more suitable because you can iterate over each letter, there’s no need to create an integer to track the position - just ask the string to provide its characters one at a time (that’s what a for-loop does)

for char in text:
    ...

With this:

len(text) - i > 0

I have to think long and hard about whether that does the right thing, I also have to look further down for i += 1 which is in two places, is it correct in both cases? Turns out it happens either way (so it shouldn’t be inside an if-statement, put it once outside instead)
Compare to the for-loop which is understandable at a glance.
A for-loop is easier for counting up from zero as well, because you can create a range and iterate through that.

You have a while-else. That’s probably not what you meant - take great care that each thing in your code is necessary, because if it’s not, then what could it possibly be doing? In this case it doesn’t do anything at all (so it shouldn’t be there). To run code after a loop, put that code below the loop.



word = word + text[i]

This (the + operator) says to copy the whole current word.
If you do this 50 times, then on average the word is 25 characters, meaning 25 characters are copied 50 times, that’s 1250 copied characters. You wouldn’t do that many steps manually, this isn’t efficient!
What you would do instead is use a data structure that does support appending (list for example), and then create a string from that.


#5

Thank you for the feedback. I’m new to coding and absolutely loving it, but I really need advice like this. I especially need readability critiques.

Huge help. I can see now why the codecademy version of this code is preferable.

glad this community is so helpful!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.