Word=input('please enter a word')
def cap(word):
for char in word:
if char in 'aeiou':
return letter.upper()
else:
return letter
result=cap(word)
print result
Asked
Active
Viewed 1,150 times
-2
-
3Because you can only return from a given function invocation once. – jonrsharpe Jul 07 '19 at 20:58
-
1Ohhh. Please how do I make it iterate? – Flahmez Jul 07 '19 at 20:59
-
Build a single result to return. Also I'd suggest upgrading to Python 3. – jonrsharpe Jul 07 '19 at 20:59
-
Collect the letters in a list or append to string and finally (after the loop) return that. – Michael Butscher Jul 07 '19 at 21:00
-
I use python 3. Here is the question: Build a function to capitalize every vowel in a string – Flahmez Jul 07 '19 at 21:00
-
what do you actually want to achieve? – AAA Jul 07 '19 at 21:01
-
1Along with the main issue, you're iteration variable is `char` but you're returning `letter`. Also `Word != word` – Mark Jul 07 '19 at 21:01
-
To capitalize every vowel in an input – Flahmez Jul 07 '19 at 21:02
-
Ohhh. That was a mistake, I'd change that now. Thanks – Flahmez Jul 07 '19 at 21:04
2 Answers
3
You return immediately after examining the first character. Instead, you should go over all of them, and modify the ones you need.
def cap(word):
result = ''
for letter in word:
if letter in 'aeiou':
result += letter.upper()
else:
result += letter
return result
Note, however, that this may be much easier to do with list comprehensions:
def cap(word):
return ''.join(l.upper() if l in 'aeiou' else l for l in word)

Mureinik
- 297,002
- 52
- 306
- 350
0
In python you can have functions that continuously return values — they're called generators. You just use yield
instead of return. You can then use them as iterators, or call list
on them to get the values:
word=input('please enter a word')
def cap(word):
for letter in word:
if letter in 'aeiou':
yield letter.upper()
else:
yield letter
result=cap(word)
print(''.join(list(result)))
However, if your goal is to translate a set of characters to another set of characters, there is a python string method for that: translate
().
word=input('please enter a word')
upper_vowels = word.translate(str.maketrans('aeiou', 'AEIOU'))
print(upper_vowels)
This should be more efficient that looping and joining as well being easier to read. Also, you can save the translate table separately if you want to apply it to many strings.

Mark
- 90,562
- 7
- 108
- 148