如何计算Python中的元音和辅音?
我试图编写一个程序,用Python计算元音和辅音,然后在两个语句中打印元音和辅音的数量。元音和辅音必须具有两种不同的功能。我已经完成了大部分,但我不能找出两个错误 1.)如何阻止我的脚本为每个元音计数打印新行。我尝试过多种不同的累加器和打印语句,但似乎都不起作用 2.)我根本无法运行countConsonants函数。我想我会有类似的问题,第一,但我不能让它甚至运行。我假设这与我从主函数调用函数的方式有关,但我不确定 以下是我所拥有的:如何计算Python中的元音和辅音?,python,Python,我试图编写一个程序,用Python计算元音和辅音,然后在两个语句中打印元音和辅音的数量。元音和辅音必须具有两种不同的功能。我已经完成了大部分,但我不能找出两个错误 1.)如何阻止我的脚本为每个元音计数打印新行。我尝试过多种不同的累加器和打印语句,但似乎都不起作用 2.)我根本无法运行countConsonants函数。我想我会有类似的问题,第一,但我不能让它甚至运行。我假设这与我从主函数调用函数的方式有关,但我不确定 以下是我所拥有的: def main(): user_input = i
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()
我通读了你的代码,发现了几个问题。您似乎没有在正确的位置调用
。lower
。它不修改字符串,只返回字符串的小写版本。我把你的元音和辅音和数学结合起来。此外,我还添加了一个条件for循环,它将扫描字母并挑出所有元音,然后它将获取找到的元音列表的长度。最后,我还将元音列表
组合成一个字符串,使其看起来更漂亮
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()
缩进是关键
打印只能在while循环完成后进行,因此必须与while缩进相同。索引的增量也在错误的位置:无论if
条件的计算结果是否为True,每次都必须发生这种情况。(在对齐时,索引只会增加过去的元音,并且可能永远不会足够远,以允许while
循环结束;这就是为什么您永远不会得到countconsonates
)
然后,您的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循环替换了while循环,并添加了一个带有辅音列表的变量count_辅音
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()
定义一个过程
是元音
它将您的姓名作为输入并打印:
‘Your name starts with avowel’
如果它以元音开头,并打印出“你的名字以辅音开头”
,否则?
例如:
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’
这是一个计算元音和辅音的程序
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()
它使用字典切片:
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
"""
打印所有结果
您需要重新分配用户输入
,否则它将不会保留小写。只需将print
行与while
行放在同一级别,这样每次函数调用都会打印一次。此外,您可以在字符串上迭代,检查元音列表中的每个字符是否,
尝试将两个函数中的索引+=1
按1个选项卡删除,并将两个函数中的print语句按2个选项卡删除。您的直觉是正确的。您的代码更简单、更高效。不幸的是,我被授权将它们分成两个独立的函数。谢谢@ramcdougal,你对缩进的理解是正确的。我尝试将print语句缩减到while位置,但仍然出现错误,因此我将其移回。直到我减少了索引累加器,它才起作用。谢谢你的帮助。请为你的答案提供一些解释。我编辑了它,希望它现在对你有帮助–@SilidroneYeah它很好:D只是你现在做得太过分了,可能比以前更好,你可以用一两句话解释代码,不需要注释每一行代码。我撤销了我的否决票,并感谢你的指导,我是堆栈溢出的新手–@Silidrone
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
"""
##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)