def every_other_letter(word):
new_word = []
word_split = word.split()
for i in range(len(word), 2):
new_word.append(word_split[i])
return "".join(new_word)
It returns “IndexError: list index out of range” on line. I tried to correct the range but I was not able to. Is it possible to keep the same logic (even though I’m aware this is not the best way to solve the exercise) just correcting the range?
Something to note, split() does not split a word, but a sentence. To split a word we would use, list(). That brings up the question, why even bother since a string is iterable, already?
Could we not simply iterate the string and build a new string from the letters we extract?
s = ""
...
s += word[i]
Now the range() issue. The step is a positional argument, meaning it has to be in the third position.
Hi,
My code is working even without using range. Below is my program-
def every_other_letter(word):
return word[::2]
# Uncomment these function calls to test your function:
print(every_other_letter("Codecademy"))
# should print Cdcdm
print(every_other_letter("Hello world!"))
# should print Hlowrd
print(every_other_letter(""))
# should print
My query is : Is it necessary to use range and for loop in this program ?
At this point the more practice and application the more we learn about special cases and alternate usage. Shortcuts are fine, but not if they are not backed up by clear understanding of range and loops.
What is wrong with my code, I think it is correct. Displays the correct results, yet codecademy gives me a red cross? I am missing something, do help. Here is my code.
def every_other_letter(word):
new_word = " "
for i in range(0, len(word), 2):
new_word = new_word + word[i]
return new_word
# Uncomment these function calls to test your function:
print(every_other_letter("Codecademy"))
# should print Cdcdm
print(every_other_letter("Hello world!"))
# should print Hlowrd
print(every_other_letter(""))
# should print
I don’t understand the goal of this exercise. Even with the examples given, I really don’t get it. Can someone reformulate the question of this challenge?
Thank you so much
This exercise is asking you to return a string containing every other letter of a given input.
For example, take this function call.
every_other_letter("challenge") # should return calne
We start with the first letter, c. Then we skip a letter to get to the next one a. We repeat this until we reach the end of the string. Now we want to return a string all of the letters that were not skipped (c, a, l, n, e).