如何计算Python中的元音和辅音

2024-04-20 12:08:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个程序,它接受用户输入的句子,并在python中输出元音和辅音的计数。我的元音计数器工作正常,但辅音计数总是返回0。我不知道为什么

def checkvowelsConsonants():
    consonants = 0
    vowelscount = 0
    index = 0
    sentence = input("Please input your sentence here. This sentence must contain a speical character eg, - ? etc.")
    Vowels = (["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"])
    vowelcount = 0
    while index < len(sentence):
        if sentence[index] in Vowels:
            vowelcount += 1
        index += 1
    while index < len(sentence):
        if sentence[index] not in Vowels:
            consonants += 1
        index += 1
    print("The number of vowels is", vowelcount, "and consonants is", consonants)

这是我收到的一个输出,供参考

请在这里输入你的句子。这个句子必须包含一个特殊字符,例如,-?等等,aaaaaassssss 元音的数量是8,辅音的数量是0


Tags: ininputindexlenifissentence句子
3条回答
def classify_letter(string: str):
    vowels = 0
    consonants = 0
    for i in string:
        if i.casefold() in 'aeiou':
            vowels += 1
        elif i.casefold() in 'qwrtypsdfghjklzxcvbnm':
            consonants += 1
    return vowels, consonants

或者用一个列表

def classify_letter(strng: str) -> tuple:
    x = [i.casefold() in "aeiou" for i in strng if i.casefold() in string.ascii_lowercase]
    return x.count(True), x.count(False)

^我承认这可能不是实现这一点的最佳方式,我愿意听取建议,使其更简短、更优化

当传递"hello-"时,这两个函数都返回:

(2,3)

正如here所指出的,问题在于index变量在计算元音后不会重置,因此程序不会再次迭代字符串来计算辅音

事实上,index变量应该完全删除。代码还有一些其他问题:最重要的一个问题是,你把任何不是元音的东西都算作辅音。字符串','不是元音,但肯定不是辅音

以下是您的程序的重构版本:

def count_vowels_consonants(str_in: str):
    vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
    consonants = {'w', 'm', 'j', 'r', 'z', 'c', 'h', 'l', 'p', 'k', 's', 'b', 'y', 'n', 'f', 'q', 't', 'd', 'x', 'g',
                  'v', 'L', 'S', 'N', 'Q', 'F', 'B', 'R', 'Z', 'X', 'J', 'D', 'H', 'V', 'C', 'P', 'G', 'K', 'T', 'Y',
                  'M', 'W'}
    num_vowels = 0
    num_consonants = 0

    for char in str_in:
        if char in vowels:
            num_vowels += 1
        elif char in consonants:
            num_consonants += 1

    return num_vowels, num_consonants


sentence = input('Please input your sentence (this sentence must contain a speical character eg, - ? etc.): ')

sentence_num_vowels, sentence_num_consonants = count_vowels_consonants(sentence)

print('vowels: {}, consonants: {}'.format(sentence_num_vowels, sentence_num_consonants))

我还改进了名称,并移动了代码,以确保函数纯粹且易于重用

这里的问题是,在计算元音后,您没有重置索引变量。因此,当它到达辅音循环时,索引变量已经被设置为句子长度的任意值,并且辅音循环被跳过。像这样的东西可以解决这个问题:

def checkvowelsConsonants():
    consonants = 0
    vowelscount = 0
    index = 0
    sentence = input("Please input your sentence here. This sentence must contain a speical character eg, - ? etc.")
    Vowels = (["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"])
    vowelcount = 0
    while index < len(sentence):
        if sentence[index] in Vowels:
            vowelcount += 1
        index += 1
    index = 0
    while index < len(sentence):
        if sentence[index] not in Vowels:
            consonants += 1
        index += 1
    print("The number of vowels is", vowelcount, "and consonants is", consonants)

请注意,在第一个循环之后 index = 0 线 这将重置变量,以便它可以再次通过循环

相关问题 更多 >