【发布时间】:2014-11-12 23:46:12
【问题描述】:
我正在尝试创建一个程序来计算给定句子中元音的数量并返回最常见的元音和它(它们)出现的次数以及最不常见的元音( s) 同时忽略那些根本不发生的。 这是我当前的代码
import collections, string
print("""This program will take a sentence input by the user and work out
the least common vowel in the sentence, vowels being A, E, I, O and U.
""")
sent = None
while sent == None or "":
try:
sent = input("Please enter a sentence: ").lower()
except ValueError:
print("That wasn't a valid string, please try again")
continue
punct = str(set(string.punctuation))
words = sent.split()
words = [''.join(c for c in s if c not in string.punctuation) for s in words]
a = 0
e = 0
i = 0
o = 0
u = 0
for c in sent:
if c is "a":
a = a + 1
if c is "e":
e = e + 1
if c is "i":
i = i + 1
if c is "o":
o = o + 1
if c is "u":
u = u + 1
aeiou = {"a":a, "e":e, "i":i, "o":o, "u":u}
print("The most common occuring vowel(s) was: ", max(aeiou, key=aeiou.get))
print("The least common occuring vowel(s) was: ", min(aeiou, key=aeiou.get))
ender = input("Please press enter to end")
目前,它打印出出现次数最多和最少的元音,而不是全部,它也不会打印出现次数,也不会忽略那些根本不出现的元音。任何有关我将如何去做的帮助将不胜感激。
谢谢
【问题讨论】: