Feb-07-2023, 07:22 PM
(This post was last modified: Feb-07-2023, 10:02 PM by deanhystad.)
What do you mean by " I have to use the function they gave me, I am not supposed to change it"? You wrote the "replace_ending()" function in your original post. You can change that code, correct?
replace_ending() has a bug. The function is supposed to replace old with new, but only if old is at the end of the sentence. Your function does not test if old is at the end of the sentence. It only tests if old is in the sentence. That is why you have a problem with:
Your code creates a funny sentence ending with "thedonuts" because "seashells" has 9 characters and "seashore" has 8. Your function blindly removes the last 9 characters from the sentence; " seashore" (notice the space at the front) and appends "donuts".
Fix your function so it is correct, and it will pass all the tests.
replace_ending() has a bug. The function is supposed to replace old with new, but only if old is at the end of the sentence. Your function does not test if old is at the end of the sentence. It only tests if old is in the sentence. That is why you have a problem with:
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts"))There should be no substitution because "seashells" is not the last word in the sentence.
Your code creates a funny sentence ending with "thedonuts" because "seashells" has 9 characters and "seashore" has 8. Your function blindly removes the last 9 characters from the sentence; " seashore" (notice the space at the front) and appends "donuts".
Fix your function so it is correct, and it will pass all the tests.