Anti_vowel boolean troubles and no print out


I know that there are other ways to solve this exercise but I am really interested in learning about what is going wrong in this code. I have two problems:
1) when I use the print True/False to test the if statement with anti_vowel('ABC') I get each character to return True even though A should return False.
2) I am not getting any output from the print new_text command.

I had expected new_text to be a string of concatenated non-vowel characters

Thank you so much for your help, I have been going back and forth and I really want to understand what is wrong!

def anti_vowel(text):
    original_text = str(text.upper())
    new_text = ''
    for n in range(0, len(original_text)): 
        if original_text[n] != 'A'or 'O' or 'I' or 'U' or 'E':
            new_text = new_text + original_text[n]
            print True
            print False
    return new_text
    print new_text

Anti-vowel issues

Logical or short-circuits on truthy. 'O' is truthy so this expression will always yield True since it is hard coded into the expression. The remaining portion of the code is never reached.

A compiled language might be able to correctly interpret that, but a script interpreter needs more declarative expressions.

x != A or x != B or x != C

Each operand in the logical expression is a complete expression unto itself.

It's good that you're taking this route to explore the possibilities, just so long as you come back around and refactor once you understand the intricacies of your test case.

if x.lower() not in 'aeiou':

While you are experimenting, here are a couple of concepts to follow up on in your travels.

Using a lambda function in scope...

def anti_vowel(text):
    nov = lambda x: x if x.lower() not in 'aeiou' else ''
    result = ''
    for k in text:
        result += nov(k)
    return result

print (anti_vowel("Take the long way home"))   # Tk th lng wy hm

and using a list comprehension...

def anti_vowl(text):
    return ''.join([x if x.lower() not in 'aeiou' else '' for x in text])

print (anti_vowl("Take the long way home"))    # Tk th lng wy hm

This is the sort of stuff we derive from doing the full blown approach you are working on. It's not what we start with, but what results from continual review and study of our code. Once we fine tune our logic, the picture unfolds. That is what refactoring is. We need something to refactor to begin with. Don't change how you are learning. It will lead to this, and much more if you stay that course.


Not to leave anything out of your projected studies, let's look at regular expressions. I know this is further down the road for both of us, but it's worth a gander, just so long as we don't try to use it here.

def no_vowel(text):
    import re
    v = re.compile('[aeiou]+')
    return re.sub(v, '', text)

print (no_vowel("Take the long way home"))    # Tk th lng wy hm


import re
vowels = re.compile('[aeiou]+')

def no_vowel(text):
    global vowels
    return re.sub(vowels, '', text)

print (no_vowel("Take the long way home"))    # Tk th lng wy hm

This post comes with a proviso. The approach is extremely resource intensive, therefore wasteful in a one-off situation. When parsing huge amounts of data, though, this is a viable approach. Regular Expression engines are designed to chew through data like nothing else. When the size of the data, or the repetitive need is present, then this is the one to reach for. Otherwise, use the built-ins to your advantage.


Thank you so much, that was incredibly helpful and encouraging! Wow, the concept of truthy is mind-blowing! Thank you for solving my mystery, I really thought I was going crazy. I will check out the lamb functions and list comprehension approaches, thank you for the tips.


truthy has a counterpart, falsy. These apply to values and expressions that are not themselves a boolean, but can be cast to a boolean.

You will by now have learned that all expressions yield a value. It's the value that is truthy or falsy, not the expression itself. The value is what the computer sees, not the expression.

Truthy values are any that are non-zero, not the empty string, and not None. Falsy are the opposite of course, so, 0, ""/'', and, None are falsy.


A post was split to a new topic: It shows error


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