Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 这个变量如何知道该做什么_Python_Variables_Boolean Expression - Fatal编程技术网

Python 这个变量如何知道该做什么

Python 这个变量如何知道该做什么,python,variables,boolean-expression,Python,Variables,Boolean Expression,这是代码的一部分 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

这是代码的一部分

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
及及

它说如果不是prev_char_was_元音,那么这个和那个,但我看不出这个变量和“元音”变量之间的联系。“prev_char_was_元音”变量如何知道它需要检查前一个符号是否是元音。我看不出它以任何方式与“元音”变量联系在一起以这种方式表现


很明显,我没有看到什么。我希望这条信息足以让你理解我的问题。如果没有,我可以写下全部代码,不要对我太苛刻,我刚开始编程你的prev_char变量什么都不知道。作为程序员,您使用这个变量来存储含义,并以一种有助于告知读者它在代码上下文中可能意味着什么的方式对它进行命名

您的代码根据当前字符更改prev_char变量的值,并允许程序员根据该值编写逻辑条件(例如if语句)。prev_char变量存储true或false以及上下文中的值,即最后一个字符是否为元音。这被称为布尔值,是python中的基本类型


我强烈建议阅读/做一些/更多关于python的教程,编程很棒,这是一个好的开始

请正确设置代码的格式。缩进在Python中很重要,但目前您的代码存在严重错误,Edit没有
prev\u char\u was\u元音
只是一个
bool
对象。您将其初始化为
False
,然后在for循环中,首先检查
if char in元音:
,它检查您正在迭代的当前字符是否在该元音字符串中,如果是,则检查bool并相应地修改它。可视化您的代码执行-