Your thoughts


#1

Love your thoughts @mtf -

Can you please break down this code for me line by line, I want to understand.

def anti_vowel(text):
t="“
for c in text:
for i in “ieaouIEAOU”:
if c==i:
c=”"
else:
c=c
t=t+c
return t

Here is what I think:

def anti_vowel(text): - takes a random input in (text).
t="" - t is now a random string you can store to.
for c in text: - c now iterates through the input text.
for i in “ieaouIEAOU”: - pretty much start to get confused here…
if c==i: – I
c=""
else:
c=c
t=t+c
return t


8.anti_vowel
#2
    if c in "aeiouAEIOU":

instead of another loop. in will iterate the string and return True is a match is found.


#3
def anti_vowel(text):

The header line contains the keyword, def, an identifier variable, anti_vowel, and the formal parameter, text followed by a colon that tells the interpreter to expect a block (indented code).

    t = ""

While a valid name, it could be more descriptive; i.e.,

no_vowels = ""

Either form initializes a variable set to an empty string.

    for c in text:

iterates over text one character at a time. Again, we can use a more descriptive variable name for readability; i.e.,

    for char in text:

Here is where we use in and a conditional expression…

        if char in "aeiouAEIOU":
            continue
        no_vowels += char

and finally the return statement,

    return no_vowels

Examine your code and compare it to this approach. Recall your reasoning fo writing the code they way you did and see if you might adjust your thinking or learn another line of reasoning to add to yours.


#4

Ok I was a bit confused, as I code I gave you was the exact code from Codeacademy. I believe it doesn’t use the “continue” function because we haven’t learned that at this point.

Taking that into consideration, can you expand a tad on how this function works.

if char in “aeiouAEIOU”: - makes sense it checks if a character is one of thse.
continue - not sure what this does. By a glance it looks like it just throws it away in no mans land.
no_vowels += char - how / where are characters getting assigned or added to your string no_vowels = “”

Thanks man you rock helping people out! Coding is awesome!


#5

It was used in the OP of the referring topic to this one so I took it as moot.

continue breaks a loop iteration, without breaking the loop. No further action takes place in that iteration but the loop continues as normal.

Compound assignments are the short form of the old literal string replacement,

no_vowels = no_vowels + char

Now consider a couple other approaches…

def anti_vowel(text):
  result = []
  for x in text:
    if x.lower() not in "aeiou":
      result.append(x)
  return ''.join(result)

print (anti_vowel("Hey You!"))    # Hy Y!
def anti_vowel(text):
  return ''.join(filter(lambda x: x.lower() not in 'aeiou', text))

print (anti_vowel("Hey You!"))    # Hy Y!

LIke ‘continue’ these might have zipped right over your head. Not to worry. It was not to rub it in, but to expose you. There is no pressure and you do not have to know this stuff right now. Just know that it is coming down the pike and soon enough will be a part of your kit.


#6

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