编写一个程序,确定一个字符串python中有多少个元音和辅音

布兰登

用一个接受字符串作为参数并返回字符串包含的元音数量的函数编写程序。应用程序应具有另一个函数,该函数接受字符串作为参数并返回该字符串包含的辅音数量。该应用程序应让用户输入字符串,并应显示元音的数量及其所包含的辅音的数量。

这是我到目前为止的代码:

def main():
   mystr = input('Please enter a string: ')
   mystr.lower()
   index = 0
   vowelSet = set(['a','e','i','o','u'])
   vowels = 0
   consonants = 0

def runVowels(mystr):
    while index < len(mystr):
        if mystr[index] in vowels:
            vowels += 1
            index += 1
        print('This string consists of ' + mystr(vowels) + 'vowels')

def runConsonants(mystr):
    while index < len(mystr):
        if mystr[index] != vowels:
            consonants += 1
            index += 1
        print('This string consists of ' + mystr(consonants) + 'consonants')

main()

我在哪里出错了,我在正确的轨道上吗?

双音

下面的代码在python 2.7.3上进行了测试。

  1. 您需要研究变量作用域,不能在一种方法中定义变量,而在另一种方法中使用它。

  2. 研究一下什么是从用户那里获取输入的最佳方法,sys是一个非常好的库

始终,始终在使用变量之前初始化变量,内联提供了注释。

def main():

   mystr = raw_input("Enter your String:")
   mystr.lower()
   #index = 0, index is useless here
   vowelSet = set(['a','e','i','o','u'])
   #vowels = 0, vowels is useless here
   #consonants = 0, consonants is useless here

   #Either pass vowelSet as argument or define them explicitly in the methods
   runVowels(mystr, vowelSet)
   runConsonants(mystr, vowelSet)

def runVowels(mystr, vowelSet):
    #index, vowels needs to be defined and assigned a default value here
    index = 0
    vowels = 0

    while index < len(mystr):
        if mystr[index] in vowelSet:
            vowels += 1

        # You need to increment index outside of the condition
        index += 1
 print 'This string consists of ', vowels , 'vowels'

def runConsonants(mystr, vowelSet):
    #index, consonants needs to be defined and assigned a default value here
    index = 0
    consonants = 0
    while index < len(mystr):
        if mystr[index] not in vowelSet:
            consonants += 1

        # You need to increment index outside of the condition
        index += 1
    print 'This string consists of ' , consonants , 'consonants'

main()

样品运行:

$ python vow.py 
Enter your String:aeeiithy
This string consists of  5 vowels
This string consists of  3 consonants

同样,该程序仅打印一些元音。如果您需要数量“不同”的元音,那就有些不同了。希望这可以帮助 !

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何计算一个字符串中有多少个辅音?

查找一个字符串中有多少个条目在另一个字符串中

在C语言中,如何使用指针来计算一个字符串中有多少个元音?

计算一个字符串中有多少个子字符串是回文

输入另一个字符串后计算元音和辅音

php-区分大小写的函数计算一个字符串中有多少个字符

我的算法中确定一个字符串需要多少个字母替换为另一个字符串的字谜的算法有何缺陷?

在不同位置的其他字符串中,第一个字符串中有多少个数字

计算一个字符串中出现了多少个数字

我怎么知道一个字符串中替换了多少个匹配项?

元组列表中第一个字符串之后存在多少个整数

给定两个字符串数组,请为列表中的每个字符串确定另一个列表中有多少个字谜。如何提高时间效率?

Python连接一个字符串和一个变量

C++ 中有没有办法确定一个字符串是“实习”还是在一个只读的持久位置?

找出:在给定的句子中一个字符有多少个单词:PYTHON

编写一个程序,确定一个名字中每个字母有多少个

在system()中有一个字符串

用红宝石计数一个字符串中的元音

如何比较一个字符串和一个字符

连接一个函数和一个字符串

如何减去一个SimpleDateFormat和一个字符串

编写一个程序,它的交流字母“e”和使用`replace`方法在一个字符串中的“o”

如何在从 json 解码的 swift 字典中有一个字符串和一个 int

如何计算一个字母在一个字符串中出现多少次?C程序设计

在LIST Python中找到以元音开头的第一个字符串

比较python中字符串的第一个和最后一个字符

Python检查字符串的第一个和最后一个字符

通过合并一个int和一个字符串来创建一个字符串

具有一个字符串和一个字符串列表的相似性API