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

时间:2017-04-02 02:58:52

标签: python

我正在尝试编写一个用Python计算元音和辅音的程序,然后在两个语句中打印元音和辅音的数量。元音和辅音必须具有两种不同的功能。我已经完成了大部分内容,但我无法弄清楚两个错误。

1。)如何阻止我的脚本为每个元音计数打印一个新行。我尝试了很多累加器和打印语句的变体,但似乎都没有。

2。)我无法让我的countConsonants函数运行。我假设我会遇到类似的问题,但我无法让它运行。我假设它与我从main函数调用函数的方式有关,但我不确定。

这就是我所拥有的:

def main():
   user_input = input("Enter a string of vowels and consonants: ")
   vowel_list = set(['a','e','i','o','u'])
   countVowels(user_input, vowel_list)
   countConsonants(user_input, vowel_list)

def countVowels(user_input, vowel_list):
    user_input.lower()
    index = 0
    vowels = 0

    while index < len(user_input):
        if user_input[index] in vowel_list:
            vowels += 1
            index += 1
            print ('Your input has', vowels , 'vowels.')

def countConsonants(user_input, vowel_list):
    user_input.lower()
    index = 0
    consonants = 0

    while index < len(user_input):
        if user_input[index] not in vowel_list:
            consonants += 1
            index += 1
            print ('Your input has' , consonants , 'consonants.')

main()

这是一个IO:

Enter a string of vowels and consonants: aaeioulkjj
Your input has 1 vowels.
Your input has 2 vowels.
Your input has 3 vowels.
Your input has 4 vowels.
Your input has 5 vowels.
Your input has 6 vowels.

7 个答案:

答案 0 :(得分:2)

我仔细阅读了您的代码并发现了一些问题。你似乎没有在正确的位置调用.lower。它不会修改字符串,只返回字符串的小写版本。我将你的元音和辅音与一些数学相结合。另外,我添加了一个条件for循环,它将扫描字母并选出所有元音,然后它将占用找到的元音列表的长度。最后,我还将vowel_list组合成一个字符串,使其看起来更漂亮。

  def main():
     user_input = input("Enter a string of vowels and consonants: ").lower()
     vowel_list = 'aeiou'
     countVowelsAndConsoants(user_input, vowel_list)

  def countVowelsAndConsoants(user_input, vowel_list):
      vowels = len([char for char in user_input if char in vowel_list])
      print ('Your input has', vowels , 'vowels.')
      print ('Your input has', len(user_input)-vowels, 'consonants.')

  main()

如果你需要它们,请分开:

  def main():
     user_input = input("Enter a string of vowels and consonants: ").lower()
     vowel_list = 'aeiou'
     countVowels(user_input, vowel_list)
     countConsonants(user_input, vowel_list)

  def countVowels(user_input, vowel_list):
     vowels = len([char for char in user_input if char in vowel_list])
     print ('Your input has', vowels , 'vowels.')

  def countConsonants(user_input, vowel_list):
     vowels = len([char for char in user_input if char in vowel_list])
     print ('Your input has', len(user_input)-vowels, 'consonants.')

  main()

答案 1 :(得分:2)

缩进是关键。

打印应该只在while循环完成后才会发生,所以它必须与while相同。索引的增量也在错误的位置:无论if条件是否计算为True,每次都必须发生这种情况。 (通过对齐,索引只会增加元音,并且可能永远不会让while循环结束;这就是你永远不会countConsonants的原因。)

您的countVowels功能将变为:

def countVowels(user_input, vowel_list):
    index = 0
    vowels = 0

    while index < len(user_input):
        if user_input[index] in vowel_list:
            vowels += 1
        index += 1
    print ('Your input has', vowels , 'vowels.')

顺便说一下,考虑在for而不是user_input使用while循环代替for char in user_input: if char in vowel_list: vowels += 1 和索引;即使用类似的东西:

 Sol(indx,j) = mf* ((alpha/dx^2)*(Sol(indx+1,j-1)-2*Sol(indx,j-1)+Sol(indx-1,j-1))...
        + (K/dt)*Sol(indx,j-1) +(1/dt^2)*(2*Sol(indx,j-1) - Sol(indx,j-2)));

答案 2 :(得分:2)

##it is list
listChar = ['$','a','8','!','9','i','a','y','u','g','q','l','f','b','t']
c = 0##for count total no of elements in a list
cVowel = 0 # for count vowel
cConst = 0 # for count consonants
cOther = 0 # for count other than consonants and vowels
for i in listChar : ## use loop for count eaxh elements
    c += 1
    if i in 'aeiou' : ## check it is vowewl
        cVowel = cVowel + 1 # count vowel
    elif i in '!@#$%^&*()+-*/123456789~`' : # other than alphabets
        cOther = cOther + 1 # count other than alphabets elements
    else :
        cConst = cConst + 1 ## count consonants if above contion not satisfied

打印所有结果

print ("total number of element in the list  : ", c)
print("count vowels characters : ",cVowel)
print("count consonants characters : ",cConst)
print("count other characters : ",cOther)

答案 3 :(得分:1)

我希望这就是你想要的。我用for循环替换了while循环,并添加了一个带有辅音列表的变量count_Consonant。

def countVowels(user_input, vowel_list):
    vowels = 0
    for i in vowel_list:
        if i in user_input:
            vowels+=1
            print ('Your input has {} vowels.'.format(vowels))

def countConsonants(user_input, count_Consonants):
    consonants = 0
    for i in count_Consonants:
        if i in user_input:
            consonants+=1
            print ('Your input has {} consonants.'.format(consonants))

def main():
   user_input = input("Enter a string of vowels and consonants:  ").lower()
   vowel_list = set(['a','e','i','o','u'])
   countVowels(user_input, vowel_list)
   count_Consonants = set(["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"]) 
   countConsonants(user_input, count_Consonants)

main()

答案 4 :(得分:0)

def VowCons(Str):
    Vcount = 0
    ConsCount = 0
    Vowels = ['a','e','i','o','u']
    for char in Str:
        if char in Vowels:
            Vcount +=1
        else:
            ConsCount +=1
    print(Vcount," is the number of vowels and ",ConsCount," is the count of consonants")

VowCons("how many vowels and consonants are there")

答案 5 :(得分:0)

定义过程is_vowel

以您的姓名为输入并打印:

 ‘Your name starts with avowel’

是否以元音开头并打印‘Your name starts with a consonant’,否则? 示例:

is_vowel(‘Ali’) ‘Your name starts with a vowel’
is_vowel(‘aqsa’)  ‘Your name starts with a vowel’
is_vowel(‘Sam’) ‘Your name starts with a consonant’

答案 6 :(得分:0)

这是一个计算元音和辅音的程序。

它利用字典切片:

itemgetter(*vowel_or_consonant)(c) # slice

脚本:

from collections import Counter
from operator import itemgetter
from string import ascii_letters

VOW = set('aeiouAEIOU')
CON = set(ascii_letters)-VOW

def letter_count(s, vowel_or_consonant):
    c = Counter(s)
    return sum(itemgetter(*vowel_or_consonant)(c))

s ="What a wonderful day to program"

print('Vowel count =', letter_count(s, VOW))
print('Consonant count =', letter_count(s, CON))

"""
================= RESTART: C:/Old_Data/python/vowel_counter.py =================
Vowel count = 9
Consonant count = 17
"""