I want the function to return two types of value, string and integer when counting vowels in a word. Upon invoking I'm getting only the y value, but not the x. Can you explain why is this happening?
def cnt_vow(s):
x = 0
y = ''
for char in s:
if char in 'aeuio':
y = y + char
x = x + 1
return y
return x
cnt_vow('hello')
expected: 'eo', 2
was: 'eo'
3 Answers 3
If you want both, try returning a tuple
return x,y
return always leaves the current function, so code after it will never be executed.
You'll have to use a tuple to return multiple values:
return (y, x)
where the parenthesis are usually optional.
When calling this function, you can then unpack the two values again:
vowels, count = cnt_vow('hello')
Once python sees a return statement the function execution ends, a second statement is never reached.
Comments
Once your code hits the first return statement, it returns the specified value immediately.
What you're looking for is:
return y, x