-3

This is some part of the code

def count_syllables_in_word(word):
    count = 0
    ... 
    vowels = "aeiouAEIOU"
    prev_char_was_vowel = False

    for char in word:
        if char in vowels:
            if not prev_char_was_vowel:
                count = count + 1
            prev_char_was_vowel = True

        else:
            prev_char_was_vowel = False

And and and

It says if not prev_char_was_vowel then this and that but I don't see the connection between this variable and the "vowels" variable. How does the "prev_char_was_vowel" variable know it needs to check if the previous sign is a vowel. I don't see it linked in any way with the "vowels" variable to behave this way.

Clearly I'm not seeing something here. I hope this piece of information is enough for you to understand my question. If not I can write down the whole code Don't judge me too harshly I just started with programming

Adam
  • 709
  • 4
  • 16
Aria
  • 1
  • 1
  • 6
    Please `format your code` correctly. Indentation matters in Python, but currently your code is heavily misindented – ForceBru Feb 05 '20 at 19:06
  • 1
    It doesn't. `prev_char_was_vowel` is just a `bool` object. You initialize it to `False`, then inside your for-loop, first check if `if char in vowels:`, which checks if the current character you are iterating over is in that string of vowels, *and if so*, you check your bool and modify it accordingly. – juanpa.arrivillaga Feb 05 '20 at 19:09
  • Visualize your code execution - http://pythontutor.com/visualize.html#mode=edit – wwii Feb 05 '20 at 19:42

1 Answers1

0

Your prev_char variable doesn't know anything. As the programmer you are using this variable to store meaning and have named it in a way which helpfully informs the reader of what it's could mean in context of the code.

Your code changes the value of the prev_char variable based on the current character and allows the programmer to write logic conditions (e.g if statements) based on this value. The prev_char variable stores true or false and the in this context that is whether the last character was a vowel. This is known as a boolean and is a basic type in python.

I highly suggest reading/doing some/more tutorials on python, programming is awesome and this is a good start!

Object object
  • 1,939
  • 10
  • 19