Reputation: 13
I'm trying to develop function mirror() that takes a string and returns its mirror image but only if the mirror image can be represented using letter in the alphabet.
>>>mirror('vow')
'wov'
>>>mirror('bed')
'INVALID'
My code doesn't give me correct answers. Thanks for any tips!
def mirror(word):
a={'b':'d','d':'b', 'i':'i', 'o':'o','v':'v','w':'w','x':'x'}
res=''
for letter in word:
if letter in a:
res=res+a[letter]
return res
else:
return 'INVALID'
return res
Upvotes: 0
Views: 735
Reputation: 33
This should work
def mirror(word):
a={'b':'d','d':'b', 'i':'i', 'o':'o','v':'v','w':'w','x':'x'}
res=''
for letter in word:
if letter in a:
res += a[letter]
else:
return 'INVALID'
return res
print(mirror("bob"))
return will break out of your function so once it finds the first letter it will stop running.
I also changed
res=res+a[letter]
to....
res += a[letter]
Upvotes: 0
Reputation: 81
The return res
in the if statement needs to be removed, the program currently exits if the first letter is a match and returns that.
Upvotes: 2