Reputation: 37
I have two functions, the first one is related to the second, and it codes for whether or not a letter is a vowel (True) or a consonant (False).
def vowel(c):
"""(str) -> bool
Return whether the string c is a vowel.
>>> vowel('e')
True
>>> vowel('t')
False
"""
for char in c:
if char.lower() in 'aeiou':
return True
else:
return False
def repeated(s, k):
"""(str) -> str
Return a string where consonants in the string s is repeated k times.
>>> repeated('', 24)
''
>>> repeated('eoa', 2)
'eoa'
>>> repeated('m', 5)
'mmmmm'
>>> repeated('choice', 4)
'cccchhhhoicccce'
"""
result = ''
for c in s:
if c is not vowel(c):
result = result + (c * k)
return result
This is what I have for the function, but the examples fail and doesn't skip the vowels.
repeat('eoa', 2)
Expected:
'eoa'
Got:
'eeooaa'
Thanks in advance!
Upvotes: 0
Views: 382
Reputation: 160587
Two things. In the vowel
function, there's no need for a loop. You're sending a single character so you simply need to check it:
def vowel(c):
if c.lower() in 'aeiou':
return True
else:
return False
Or:
def vowel(c):
return True if c.lower() in 'aeiou' else False
Then, in repeated
, don't use c is not vowel(c)
. That compares if the identity of c
, the character, is or isn't equal to True/False
. Just use the value returned from vowel
directly and conditionally add to result
:
def repeated(s, k):
result = ''
for c in s:
if not vowel(c):
result += (c * k)
else:
result += c
return result
Upvotes: 0