编写函数startWithVowel(word),该函数将单词作为参数并返回一个子字符串,该子字符串以单词中找到的第一个元音开头。如果单词中不包含元音,该函数将返回“ No vo元”。
以下代码运行正常,但我想提高自己的技能。有提示吗?
def startWithVowel(word):
vowwel='aeiou'
c=''
x=False
for l in word :
if any(y==l for y in vowwel):
x=True
if x==True:
c+=l
if x==False:
c= 'No vowel'
return c
答案 0 :(得分:0)
由于您有any
,因此不需要使用多余的布尔值。这将使它更快,更可重用:
def start_with_vowel(word):
vowwel='aeiou'
c=''
for l in word :
if any(y==l for y in vowwel):
c+=l
else:
c= 'No vowel'
return c
答案 1 :(得分:0)
def start_with_vowel(word):
vowel = 'aeiou'
for s in word.lower() :
if any(y==s for y in vowel):
return word[word.lower().index(s):]
return 'No Vowel'
assert start_with_vowel('') =='No Vowel'
assert start_with_vowel('Iota') =='Iota'
assert start_with_vowel('lOllipop') =='Ollipop'
assert start_with_vowel('Bagheera') =='agheera'
assert start_with_vowel('crwth') =='No Vowel'