编写程序统计字符串中的元音和辅音数量
写一个程序,里面有一个函数,这个函数接收一个字符串作为输入,然后返回这个字符串中包含的元音字母的数量。程序还应该有另一个函数,同样接收一个字符串作为输入,返回这个字符串中包含的辅音字母的数量。用户可以输入一个字符串,程序会显示这个字符串中元音和辅音的数量。
这是我目前写的代码:
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()
我哪里出错了?我这样做对吗?
5 个回答
在编程中,有时候我们会遇到一些问题,比如代码运行不正常或者出现错误。这种时候,我们可以去一些技术论坛,比如StackOverflow,去寻找解决方案或者询问其他人的意见。
在这些论坛上,很多人会分享他们的经验和解决方法。你可以看到各种各样的问题和答案,有些是很简单的,有些则比较复杂。对于初学者来说,理解这些内容可能会有点困难,但只要慢慢来,多看多练,就能逐渐掌握。
总之,技术论坛是一个很好的资源,可以帮助你解决编程中遇到的各种问题。记得多去看看,学习别人的经验哦!
Def test_case1(*arg):
arg=eval(input("enter the object:")
vowels=0
for x1 in arg:
if(x1 in("AEIOUaeiou")):
Vowels+=1
Print("vowels:",x1)
consent=0
for y1 in arg:
if(y1 not in("AEIOUaeiou")):
Consents+=1
print ("consent:",y1):
if(___name__=="__main__"):
test_case1("arg")
别把这个例子当成你的课堂教材哦 ;-)
看看这个可能会帮助你让自己的代码正常运行。
def charCounts(mystr):
mystr = mystr.strip().lower()
vowels = 0
cons = 0
for c in mystr:
if c in 'aeiou':
vowels += 1
elif c >= 'a' and c <= 'z':
cons += 1
return vowels, cons
if __name__ == '__main__':
mystr = input('Please enter a string: ')
vowels, cons = charCounts(mystr)
print('This string consists of {0} vowels and {1} consonants'.format(vowels, cons))
你离目标越来越近了。不过,你的循环里还有个神秘的 mystr.lower()
。这个应该放到循环外面去,而且你需要把它返回的结果(也就是小写版本的mystr)保存起来,比如 mystr = mystr.lower()
。我建议你在main()里做这个,然后把小写的版本传给你的计数函数。
一旦你这样做了,runVowels() 就几乎完美了。runConsonants() 还需要再调整一下。如果一个字符不是元音字母,并不意味着它一定是辅音字母——它可能是数字、标点符号,或者是空格。
如果你的函数中的循环直接遍历字符串本身,那样会更符合Python的风格,你不需要使用索引。可以在解释器里试试这个:
mystr = "This is a TEST string"
for letter in mystr:
print(letter)
另外,问题还说明你的每个计数函数应该返回一个数字。所以 runVowels() 和 runConsonants() 应该把它们的计数结果返回给 main(),然后由 main() 来处理打印的部分。
你这里有几个问题。
你定义了叫
myVowels
和myConsonants
的函数,但你从来没有调用它们。你可能想在main
函数的最后调用它们。在
main
函数里,mystr.lower()
并没有做什么有用的事情。这个函数会返回一个新的字符串,内容是mystr
的小写形式,但你没有把这个新字符串存起来。你需要把它存到某个地方(可以是重新存回mystr
,或者存到一个新变量里),这样你才能使用它。在
runVowels
函数里,index += 1
这一行放在了if
语句里面。所以,一旦你找到一个辅音,你就会跳过这个if
,导致index
不会增加,然后就会一直循环同一个辅音。把这一行缩进调整一下。(在runConsonants
里也有同样的问题,后面所有类似的问题也是如此。)在
runVowels
函数里,print
的调用放在了while
语句里面,这样会导致每个字母都打印一次当前的总数,而不是最后只打印一次总数。再次调整缩进。你在
main
函数里创建了名为index
、vowels
和vowelsSet
的变量,这意味着它们只在这个函数里有效。然后你在runVowels
里尝试访问这些变量,但它们并不存在。每个函数都有自己的局部命名空间。把这些赋值从main
移到runVowels
,或者像传递mystr
那样把它们传递给runVowels
。你创建了名为
vowels
和vowelsSet
的变量,但你尝试把它们当作同一个名字vowels
来访问。要理清楚,给正确的值用正确的名字。我不太明白
mystr(vowels)
是想干什么。你不能像调用函数那样调用一个字符串。我觉得你这里想用的是内置的str
函数。(不过,你可能想看看字符串格式化,或者看看当你给print
传多个参数时会发生什么;你很少需要像现在这样拼接字符串。)
我不能保证修复所有这些问题就能让你的代码按你想要的方式运行——这当然是必要的,但可能还不够。
不过,希望理解每个问题的所在能帮助你学会如何自己发现类似的问题(而不是如何避免它们——除非你是历史上最伟大的天才,否则你会一直写出这样的错误,直到你去世,你只会在测试、调试和修复这些错误的能力上变得更好)。
下面的代码是在python 2.7.3上测试过的。
你需要了解变量的作用域,也就是说你不能在一个方法里定义一个变量,然后在另一个方法里使用它。
了解一下从用户那里获取输入的最佳方法,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
再说一次,这个程序只会打印出元音字母的数量。如果你需要“不同”的元音字母数量,那就会稍微不同一些。希望这对你有帮助!