Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with test
#6
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:
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.
Reply


Messages In This Thread
Help with test - by Jacopo_98 - Feb-07-2023, 05:04 PM
RE: Help with test - by jefsummers - Feb-07-2023, 05:26 PM
RE: Help with test - by rob101 - Feb-07-2023, 05:38 PM
RE: Help with test - by Jacopo_98 - Feb-07-2023, 07:18 PM
RE: Help with test - by Jacopo_98 - Feb-07-2023, 07:13 PM
RE: Help with test - by deanhystad - Feb-07-2023, 07:22 PM
RE: Help with test - by Jacopo_98 - Feb-08-2023, 12:06 AM
RE: Help with test - by rob101 - Feb-07-2023, 07:27 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020