The purpose of this code is to make the odd placed letters uppercase and the odd ones lower cased, I’ve tried about everything I can think of and it doesnt work.
def myfunc(word):
new_word = ''
for let in word:
if (word.index(let) + 1) % 2 == 0:
new_word += let.upper()
elif (word.index(let) + 1) % 2 == 1:
new_word += let.lower()
return new_word
The code works fine for the first few but when it gets towards the end it looses its brain cells.
Input
myfunc('Anthropomorphism')
Output
'aNtHrOpOmOrpHIsm'
Expected Output:
'aNtHrOpOmOrPhIsM'
Any ideas why it doesnt work?
>Solution :
word.index(let) will get you the first index of that letter, not the current one as you iterate. Use for index, letter in enumerate(word): instead.
def myfunc(word):
new_word = ''
for index, let in enumerate(word):
if (index + 1) % 2 == 0:
new_word += let.upper()
elif (index + 1) % 2 == 1:
new_word += let.lower()
return new_word