8/15 - Anti_vowel bug?


#1

Hey guys,

So there's a weird error that's been happening and i can't seem to figure out where it's coming from.
By running the function, i mysteriously seem to be getting an extra bcd in my string, yet when i print it normally it come out completely fine. Is there something that i'm missing?

EDIT: if i remove the print, the beginning "Hy lk Wrds" will be removed but the "bcd" stays. Just to clarify

Thanks in advance.

https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096#

vwl = []

def anti_vowel(text):
    for i in range(len(text)):
        if text[i] in "AEIOUaeiou":
            pass
        else:
            vwl.append(text[i])
    return "".join(vwl)

#2

Hi @zerching,

Why is this statement placed outside the anti_vowel function instead of within it? ...

vwl = []

You could simplify your function by refactoring some of the code.


#3

vwl = []

Please put this variable inside your function, it worked for me.


#4

Thanks for the reply, that completely solved it. However, could i ask what the difference was between putting it the variable inside as opposed to outside the function? And how you would refactor the code to simplify it? I thought it was pretty concise as it, but I would love to learn how to write better.

Would refactoring it to simplify look something like this?

def anti_vowel(text):
    vwl = []
    for i in range(len(text)):
        if text[i] not in "AEIOUaeiou":
            vwl.append(text[i])
    return "".join(vwl)

#5

Because you initialize it as global variable but according to codecademy standards, this variable is define as local variable, so it throws an error.,
Thanks for liking my comment, :smile:


#6

Thank YOU for trying to help! Always appreciated :slight_smile:


#7

i actually disgree with @amansharma21, the problem is that codecademy needs to verify your function works correctly, to achieve this the function (function, not the whole script) is called multiply times with different strings.

because vwl = [] is outside your function, this will accumelate all constants from all the function calls. we can simply demonstrate this by calling the function multiply times:

vwl = []

def anti_vowel(text):
    for i in range(len(text)):
        if text[i] not in "AEIOUaeiou":
            vwl.append(text[i])
    return "".join(vwl)

print anti_vowel("look how")
print anti_vowel("vwl will collect")
print anti_vowel("constants from all function calls")

#8

This is very insightful. So by placing the variable inside the function, any time it is run python vwl= [] will be an empty list as opposed to an accumulating one.

Thank you very much for the explanation, it taught me something that I might not have learnt otherwise!


#9

by placing vwl inside the function, you will start with an empty list (make a new list actually) everytime you call the function.

if you place it outside the function, the list will be empty when calling the script, but if execute the function multiply times, the list will accumulate because its never emptied/re-defined


#10

For your anti_vowel function to work correctly, vwl needs to start out as an empty list every time the function is called. If you perform the initialization outside the function, and then call the function two or more times in your program, each invocation after the first one will process data from previous calls. See both of @stetim94's previous posts.

You have already made the function more concise by eliminating this ...

        if text[i] in "AEIOUaeiou":
            pass

Additional refactoring is possible. You could do this ...

    for ch in text:

... and then simplify this in various ways ...

        if text[i] not in "AEIOUaeiou":
            vwl.append(text[i])

EDITED to note @stetim94's posts.


#11

Thanks @stetim94 , for correcting me. Because of my previous experiences in solving the problems with codecademy, i thought that was the issue of local and global variable. by the way
Thanks a lot for your comment :slight_smile:


#12

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