8.anti_vowel


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/antivowel


Here’s my code:
def anti_vowel(text):
for c in text:
if c in “aeiouAEIOU”:
continue
else:
print c,


The compilor shows as result the following:
b c d H y l k W r d s !

And Codecademy tells me this is the ERROR: Your function fails on anti_vowel(“Hey look Words!”). It returns “None” when it should return “Hy lk Wrds!”.

I don’t get why is printing ‘bcd’ in the begining and why does it have spaces between words. Am I printing a list instead of a string?

<do not remove the three backticks above>

#2

And then I found this solution but I don’t get why we are iterating through the string ‘aeiouAEIOU’ and why does it work…

def anti_vowel(word):
    vowels='aeiouAEIOU'
    for c in vowels:
        word=word.replace(c,"")
    return word

#3

Rather than use a solution you found in the forums (that you do not understand) would it not be better to figure out why your first attempt did not work?

First rule of coding: Never use code that you do not understand and cannot explain.


#4

Hello mtf,

The code on my question was created by me. Do you know why my solution didin’t work? I don’t get why is printing ‘bcd’ in the begining and why does it have spaces between words. Am I printing a list instead of a string?


#5

The first example, right? The second example,

is one we should set aside for the time and focus on your first attempt to find where it went wrong. That to my mind would be more beneficial to you, the learner. Problem solving is a far better way to learn than having code explained away.


The reason it returns None is because there is no return statement. Your function should not print anything.


#6

Thanks mtf! It works! In what circumstances do I have to use print in a for loop and in what circumstances do I have to use a return? Is there a rule of thumb? Cause I’ve comminted this mistakes before and I don’t think it’s clear on my mind.


#7

When we write a utility function such as this, we should always return a result and then print the return value.

print anti_vowel("Hey look, words!")

#8

4 posts were split to a new topic: Your thoughts


#9

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