def V(sentence, vowel):
a=0
p=0
b=""
for i in sentence:
for z in i:
if z in vowel:
p=sentence.count(z)
if p>a:
a=p
b=z
return b
sentences=[]
vowels=["a", "e", "i", "o", "u", "y"]
v=input("input a sentence: ")
while v[0]!=v[-1]:
sentences.append(v)
v=input("input a sentence: ")
print("Most used vowel: ", V(sentences, vowels))
答案 0 :(得分:4)
当你应该sentence.count(z)
时,你正在做i.count(z)
。你的变量名有点令人困惑。但是sentence
是传递的句子的集合,而i
是实际的句子。
答案 1 :(得分:0)
将sentences
sentence
和vowels
作为vowel
传递是误导性的。
要计入字符串列表以获取该列表中元音的编号:
p = 0 # 1
for s in sentences: # 1
p += s.count(z) # 1
或者如果你是单行的粉丝:
p = sum([s.count(z) for s in sentences]) # 2
而不是:
for i in sentence:
for z in i:
# ...
你应该遍历vowels
,所以你只计算一次元音:
for z in vowels:
p = 0 # 1
for s in sentences: # 1
p += s.count(z) # 1
if p > a:
a = p
b = z
清理,提供:
def mostUsedVowel(sentences, vowels):
a = 0
p = 0
b = ""
for vowel in vowels:
p = 0 # 1
for s in sentences: # 1
p += s.count(vowel) # 1
if p > a:
a = p
b = vowel
return b
如果您愿意,可以将# 1
替换为# 2
- 只需记住将z
替换为vowel
。