根据一组元音测试字符串-Python
这是我的程序中的一个模块:根据一组元音测试字符串-Python,python,string,set,elements,Python,String,Set,Elements,这是我的程序中的一个模块: def runVowels(): # explains what this program does print "This program will count how many vowels and consonants are" print "in a string." # get the string to be analyzed from user stringToCount = input("Please e
def runVowels():
# explains what this program does
print "This program will count how many vowels and consonants are"
print "in a string."
# get the string to be analyzed from user
stringToCount = input("Please enter a string: ")
# convert string to all lowercase letters
stringToCount.lower()
# sets the index count to it's first number
index = 0
# a set of lowercase vowels each element will be tested against
vowelSet = set(['a','e','i','o','u'])
# sets the vowel count to 0
vowels = 0
# sets the consonant count to 0
consonants = 0
# sets the loop to run as many times as there are characters
# in the string
while index < len(stringToCount):
# if an element in the string is in the vowels
if stringToCount[index] in vowels:
# then add 1 to the vowel count
vowels += 1
index += 1
# otherwise, add 1 to the consonant count
elif stringToCount[index] != vowels:
consonants += 1
index += 1
# any other entry is invalid
else:
print "Your entry should only include letters."
getSelection()
# prints results
print "In your string, there are:"
print " " + str(vowels) + " vowels"
print " " + str(consonants) + " consonants"
# runs the main menu again
getSelection()
def run元音()
#解释此程序的功能
打印“此程序将计算有多少元音和辅音”
“以字符串形式”打印
#从用户获取要分析的字符串
stringToCount=输入(“请输入字符串:”)
#将字符串转换为所有小写字母
stringToCount.lower()
#将索引计数设置为其第一个数字
索引=0
#每个元素将测试一组小写元音
元音集=集合(['a'、'e'、'i'、'o'、'u']))
#将元音计数设置为0
元音=0
#将辅音计数设置为0
辅音=0
#将循环设置为按字符数运行
#串连
当索引
但是,当我测试这个程序时,我得到以下错误:
line 28, in runVowels
stringToCount = input("Please enter a string: ")
File "<string>", line 1
PupEman dABest
^
SyntaxError: unexpected EOF while parsing
第28行,用runVowels
stringToCount=输入(“请输入字符串:”)
文件“”,第1行
普普曼·达贝斯特
^
SyntaxError:分析时出现意外的EOF
我尝试将+1添加到“whileindex问题已解决。谢谢大家看起来您正在使用Python 2。而不是
输入(…)
。将对键入的Python表达式进行求值,这就是您得到SyntaxError的原因。看起来您正在使用Python 2。而不是输入(…)
。将计算作为Python表达式键入的内容,这也是您得到SyntaxError的原因。此外
if stringToCount[index] in vowels:
应该读
if stringToCount[index] in vowelSet:
而且
应该读
if stringToCount[index] in vowelSet:
根据建议,使用
原始输入
。此外,您不需要执行以下操作:
while index < len(stringToCount):
# if an element in the string is in the vowels
if stringToCount[index] in vowels:
# then add 1 to the vowel count
vowels += 1
index += 1
# otherwise, add 1 to the consonant count
elif stringToCount[index] != vowels:
consonants += 1
index += 1
# any other entry is invalid
else:
print "Your entry should only include letters."
getSelection()
这应该很好。在这里不需要使用
while
,而且非常非Pythonicimho
。尽可能利用Python等优秀语言的优势,让生活更轻松;) 按照建议使用原始输入
。此外,您不需要执行以下操作:
while index < len(stringToCount):
# if an element in the string is in the vowels
if stringToCount[index] in vowels:
# then add 1 to the vowel count
vowels += 1
index += 1
# otherwise, add 1 to the consonant count
elif stringToCount[index] != vowels:
consonants += 1
index += 1
# any other entry is invalid
else:
print "Your entry should only include letters."
getSelection()
这应该很好。在这里不需要使用
while
,而且非常非Pythonicimho
。尽可能利用Python等优秀语言的优势,让生活更轻松;) 您可以这样计算元音:
>>> st='Testing string against a set of vowels - Python'
>>> sum(1 for c in st if c.lower() in 'aeiou')
12
您可以对辅音执行类似的操作:
>>> sum(1 for c in st if c.lower() in 'bcdfghjklmnpqrstvwxyz')
26
您可以这样计算元音:
>>> st='Testing string against a set of vowels - Python'
>>> sum(1 for c in st if c.lower() in 'aeiou')
12
您可以对辅音执行类似的操作:
>>> sum(1 for c in st if c.lower() in 'bcdfghjklmnpqrstvwxyz')
26
下面是解决同样问题的另一种方法:
def count_vowels_consonants(s):
return (sum(1 for c in s if c.lower() in "aeiou"),
sum(1 for c in s if c.lower() in "bcdfghjklmnpqrstvwxyz"))
也就是说:
>>> count_vowels_consonants("aeiou aeiou yyy")
(10, 3)
>>> count_vowels_consonants("hello there")
(4, 6)
巨蟒真的很伟大
文件中的错误如下所示(加上一些建议): 如果希望用户键入的内容作为字符串,则应为
raw\u input
.lower()
方法返回一个字母降低的新字符串。它不会修改原始文件:
>>> a = "HELLO"
>>> a.lower()
"hello"
>>> a
"HELLO"
在这里,您可以轻松地执行以下操作:
vowelSet = set("aeiou")
注意,您也不需要严格地使用集合
,但通常它确实更有效
请不要对如此简单的陈述发表评论
而不是现在:
if stringToCount[index] in vowels:
vowels += 1
index += 1
你只要做:
if c in vowels:
vowels += 1
不完全正确。您正在检查一个字符不等于一个集合。也许你的意思是:
elif c not in vowels:
consonants += 1
但是这样就不会有其他的情况了。。。我得修正你的逻辑
以上内容更像是pythonic的写法:
print "In your string, there are: %s vowels %s consonants" % (
vowels, consonants)
不知道为什么要在那里调用它-为什么不从任何调用
run元音()
调用getSelection()
希望有帮助!享受学习这门伟大语言的乐趣。这里有另一种解决同样问题的方法:
def count_vowels_consonants(s):
return (sum(1 for c in s if c.lower() in "aeiou"),
sum(1 for c in s if c.lower() in "bcdfghjklmnpqrstvwxyz"))
也就是说:
>>> count_vowels_consonants("aeiou aeiou yyy")
(10, 3)
>>> count_vowels_consonants("hello there")
(4, 6)
巨蟒真的很伟大
文件中的错误如下所示(加上一些建议): 如果希望用户键入的内容作为字符串,则应为
raw\u input
.lower()
方法返回一个字母降低的新字符串。它不会修改原始文件:
>>> a = "HELLO"
>>> a.lower()
"hello"
>>> a
"HELLO"
在这里,您可以轻松地执行以下操作:
vowelSet = set("aeiou")
注意,您也不需要严格地使用集合
,但通常它确实更有效
请不要对如此简单的陈述发表评论
而不是现在:
if stringToCount[index] in vowels:
vowels += 1
index += 1
你只要做:
if c in vowels:
vowels += 1
不完全正确。您正在检查一个字符不等于一个集合。也许你的意思是:
elif c not in vowels:
consonants += 1
但是这样就不会有其他的情况了。。。我得修正你的逻辑
以上内容更像是pythonic的写法:
print "In your string, there are: %s vowels %s consonants" % (
vowels, consonants)
不知道为什么要在那里调用它-为什么不从任何调用
run元音()
调用getSelection()
希望有帮助!喜欢学习这门伟大的语言。呸,所有的代码都太慢了;)。显然,最快的解决方案是:
slen = len(StringToCount)
vowels = slen - len(StringToCount.translate(None, 'aeiou'))
consonants = slen - vowels
…n