0

I got stuck on this question on the Cisco training. I got to the answer, but don't understand why it works. Why does python remove the vowels after each 'continue'?

user_word = input("Enter your word: ")
user_word = user_word.upper()

for i in user_word:
    if i == "A":
        continue
    elif i == "E":
        continue
    elif i == "I":
        continue
    elif i == "O":
        continue
    elif i == "U":
        continue
    else:
        print(i)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
sofib
  • 19
  • 3
    Does this answer your question? [Example use of "continue" statement in Python?](https://stackoverflow.com/questions/8420705/example-use-of-continue-statement-in-python) – mkrieger1 Feb 02 '23 at 10:30
  • 2
    `contunue` means skip the loop. So any vowel `a, e, i, o, u` gets skipped and any consonant is printed. – D.L Feb 02 '23 at 10:31
  • I find [PythonTutor](https://pythontutor.com/visualize.html#mode=display) quite helpful in such cases. You paste your code into the field and can see how this program is executed step by step. – Mr. T Feb 02 '23 at 10:41
  • FYI you can combine your if statements: `if i in ('A','E','I','O','U'): continue` – otocan Feb 02 '23 at 10:49

3 Answers3

0

It does not "remove" them, it just does not print() them.

The loop is executed for each character in the word. If it is a vowel, the current iteration is ended (see the continue statement) and the next character is considered. If it is NOT a vowel, it is printed to the output and you can "see" it.

dacx
  • 824
  • 1
  • 9
  • 18
0

The reason for this is as follows:

contunue means skip the loop, in fact it is this:

The continue statement, also borrowed from C, continues with the next iteration of the loop

So any vowel a, e, i, o, u gets skipped and any consonant is printed.

by way of example, if you replace the continue keyword with your own function you would see this more clearly:

user_word = input("Enter your word: ")
user_word = user_word.upper()

# a new function to replace continue
def replace_continue():
    print('vowel is found')


for i in user_word:
    if i == "A":
        replace_continue()
    elif i == "E":
        replace_continue()
    elif i == "I":
        replace_continue()
    elif i == "O":
        replace_continue()
    elif i == "U":
        replace_continue()
    else:
        print(i)

try this code and it should be more obvious.

Here is a link to the docs: https://docs.python.org/3/tutorial/controlflow.html

D.L
  • 4,339
  • 5
  • 22
  • 45
  • right, so, of course, it goes through the whole code for each letter? – sofib Feb 02 '23 at 10:41
  • @sofib, exactly. it literally just skips the vowels (or anything that triggers the `continue` keyword). – D.L Feb 02 '23 at 10:44
0

The vowels do not get removed as in deleted, they just get ignored. continue is a key word, which tells the loop to go to the next iteration. If you were to put another print in the loop, outside of the if/elif statements, it becomes more visible. You may want to check the documentation for further information

for i in user_word:
    if i == "A":
        continue
    elif i == "E":
        continue
    elif i == "I":
        continue
    elif i == "O":
        continue
    elif i == "U":
        continue
    else:
        print(i)
    print('loop end')

Using the wourd 'continous' as value the above code yields :

C
loop end
N
loop end
T
loop end
N
loop end
S
loop end
Coding thermodynamist
  • 1,340
  • 1
  • 10
  • 18
jdoe
  • 99
  • 3
  • 14