我想要一个错误检查,以确保该字符串有元音输入

时间:2016-11-15 02:12:44

标签: python python-3.x python-3.5

from collections import Counter
Astring = input("Enter a word or sentence: ")
vowel = 'a' 'e' 'i' 'o' 'u'
Astring = Counter(c for c in Astring.lower() if c in vowel)
min_values = {mainv: Astring[mainv] for mainv in Astring if Astring[mainv] == min(Astring.values())}

if vowel not in Astring:
    print ("your text must contain vowels")

else:


    print("The least occuring vowel is:")
    for m in min_values:
        print("{vowel} with {occ} occurences.".format(vowel=m, occ=min_values[m]))

我希望我的代码能够在此基础上输出发生率最低的元音,我希望进行错误检查以确保字符串中有元音输入

1 个答案:

答案 0 :(得分:0)

您的示例中有很多关于格式和语法的问题,但以下工作并且与您尝试执行此操作的方式相当接近:

from collections import Counter

VOWELS = ('a', 'e', 'i', 'o', 'u')

string = raw_input("Enter a word or sentence: ")
if not any(True for vowel in VOWELS if vowel in string.lower()):
    print("your text must contain vowels")
else:
    print("The least occuring vowel is: {}".format(Counter(c for c in string.lower() if c in VOWELS).most_common()[-1]))