8/15 Help me make this work


#1

def anti_vowel(text):

no_vowels = ""
count = len(text)

while count > 0:
    if text[count] == "A" or "a":
        text[count] = ""
        count -=count
        
    elif text[count] == "E" or "e":
        text[count] = ""
        count -=count
    elif text[count] == "I" or "i":
        text[count] = ""
        count -=count
    elif text[count] == "O" or "o":
        text[count] = ""
        count -=count
    elif text[count] == "U" or "u":
        text[count] = ""
        count -=count
    else:
        count -=count

return text

print anti_vowel("Hola")

I've seen other submissions and their tiny efficient code, but I would like this solution to work. (If even possible). Please help if you have the time. "count -= count" should be "count-= 1"


#2

HI @sonbot,

There are a number of problems in your code that indicates you would benefit from going back to earlier exercises and making sure you read them well, you understand the concept very clearly and even experiment a little bit with them before moving on to the next :smile:

The lessons are not timed and learning is not a race. In the end you will walk away with the knowledge you gained and a completition badge but let me assure you the first will surely prove much more useful than the latter :smile:

I have taken your code as-is and tried to run it. I got an error message saying:

IndexError: string index out of range

This leads us to the first problem. In your if statement, you are trying to access each character of your text string using the count variable as a numerical index:

def anti_vowel(text):
    no_vowels = ""
    count = len(text)

    while count > 0:
        if text[count] == "A" or "a":
[...]

That will not work. Running the following code...

text = "hola"

count = len(text)
print "count is: " + str(count)    # this will print 4 as hola is 4 characters

print "looping..."
# looping through all characters of my_string using numerical index
for index_num in range(0,count):
    print "      index_num is: " + str(index_num),
    print "and text[index_num] is: " + str(text[index_num])

will yield:

count is: 4
looping...
      index_num is: 0 and text[index_num] is: h
      index_num is: 1 and text[index_num] is: o
      index_num is: 2 and text[index_num] is: l
      index_num is: 3 and text[index_num] is: a

In the programming world and as we see here, counting usually starts at 0...

So basically, using text[count], you are trying to access text[4] which would correspond to the fifth character of your text string. But your text string does'nt have a 5th character... That is what "index out of range" means :smile:

There are a number of easy way to do this. You did not choose the easiest one but still, as an adjustement, what you could do is this:

count = len(text) - 1

this would make your count go from 4 to 3 and text[3] would be the last character of "hola", so letter a

Doing so, you would also have to modify your while condition from
while count > 0:
to
while count >= 0:

Can you understand why? :smile:

We'll see if it will work in your code a bit further down but there are still problems left in the logic of your code:

text[count] = ""

Strings in Python are immutable by design, which means they cannot be altered or edited in place. There are technical reasons for this but nevermind those for now. Looking at the above line of code I assume the logic you used was to try to replace a vowel with nothing, as if to remove to vowel from the string, right? Unfortunately this will not work. I will then remove these lines from your code.

I also see that you had created an empty string variable named no_vowels which is a good thing because this is exactly what we need... Instead of removing a vowel from a string we will use this other empty string and send all consonants into it, thus creating the vowel-less string we need :smile:

But for now, let's look at the next line of your code:

count -= count    # if the count is 3, this is the equivalent of saying 3 - 3

This will decrement your count to zero, halting your while statement that checks if count is greater than 0. Instead what you want is to decrement count by one like so:

count -= 1

So now we have an if and four elif but none of them have a valid condition.

if text[count] == "A" or "a":

should instead be written as:

if text[count] == "A" or text[count] == "a":

Now that we have fixed our if and four elif to check if each character is a vowel and if it happens to be one, each of them decrements the count by one but doesn't do anything else. We are not doing anything with consonants. But it's ok because we'll manage them in the else like so:

else:
    no_vowels += text[count]

This will make use of the += augmented assignment operator to add the consonant to variable no_vowels. We will also have our function return no_vowels instead of returning text.

Let's string all these modifications together now and to better understand what's going on under the hood of your program, it helps to use print statements throughout your code. Kind of like if you would ask your program to explain to you what it is doing every step of the way. I encourage you doing with all of the exercises you will have problems with in the future

To see the resulting code:

CLICK HERE TO HEAD TO THE LAB :microscope:

Then press Run to see the output appear on the right side of the screen...

I will let you try and figure out why the string is reversed and how to fix it... :smile:

Good luck!


#3

This is a great answer. You are right, I've been rushing through it and taking too many breaks between lessons. I am now going back to the beginning and reinforcing my weak points. I am also going to use pen and paper more before I try to implement my code. When I get back here Ill try again then look toward your sample. Thank you for your reply on my past posts as well. You've been a great help. :smile:


#4

Hi @sonbot,

Your reply made me quite happy

I'm glad you have chosen to give yourself the time to learn. You will be rewarded with knowledge indeed but most of all, I'm sure you'll have a lot of fun :smile:

Come back anytime you need help, I'll always be glad to provide it if I can!

Here's a little trick:

Whenever you would like to paste code, since this forum supports Markdown Syntax you can use three backticks before and after your code block like so:

```
def my_func(x):
my_var = 0
if x > my_var:
sum = my_var + x
else:
print "Odelay!"
return sum
```

and it will retain proper indentation and even have nifty syntax highlighting:

def my_func(x):
    my_var = 0
    if x > my_var:
      sum = my_var + x
    else:
      print "Odelay!"
    return sum

Have fun! :smile:


#5

...
def anti_vowel(text):

no_vowels = ""

#HashTags Denotes old code 
#count = len(text)-1
count = 0 
print "Text has Length of "+ str(count)

#while count >= 0:
while count <= len(text)-1: 
    if text[count] == "A" or text[count] == "a":
        count += 1
        #count -=1
        print count
        
    elif text[count] == "E" or text[count] == "e":
        
        count += 1
        #count -=1
        print count
    elif text[count] == "I" or text[count] == "i":
      
        count += 1
        #count -=1
        print count
    elif text[count] == "O" or text[count] == "o":
      
        count += 1
        #count -=1
        print count
    elif text[count] == "U" or text[count] == "u":
        
        count += 1
        #count -=1
        print count
    else:
        
        
        no_vowels += text[count]
        count += 1
        #count -=1
        print count

return no_vowels

print anti_vowel("Hola")
...
I revisited previous lessons and came back to this loop. I know more efficient versions of this 8/15 exist but I am really happy you helped me make my version work. Thank you for your help. :grin:


#6

Good job @sonbot

I'm glad you are happy with your code. Your understanding is undoubtedly much better now! Come back whenever you need a hand, I'll be glad to help you as best I can. :grinning:

I see you have tried to use the code formatting trick but you used periods ... instead of backticks ```

I have highlighted the backtick character in blue on this US keyboard layout to help you find it :smile:


#7

A very clear guidance. Big thanks to Denisaltroy.