Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with test
#1
Hi, i'm currently following a google online course on python, and I am having truble solving a programming problem. I am studying strings at the moment. I am at the beginning of it, it is a very easy problem and probably a very dumb mistake, but I am really having trouble here. I tried rewatching the lessons and still couldn't figure it out.
It is my first time using a forum, I will try to be as clear as possible to explain my problem, hoping i'm using it the way it is supposed to be used.

The question reads:

The replace_ending function replaces the old string in a sentence with the new string, but only if the sentence ends with the old string. If there is more than one occurrence of the old string in the sentence, only the one at the end is replaced, not all of them. For example, replace_ending("abcabc", "abc", "xyz") should return abcxyz, not xyzxyz or xyzabc. The string comparison is case-sensitive, so replace_ending("abcabc", "ABC", "xyz") should return abcabc (no changes made).

This is what I have tried:


def replace_ending(sentence, old, new):
	# Check if the old string is at the end of the sentence 
	if old in sentence:
		# Using i as the slicing index, combine the part
		# of the sentence up to the matched string at the 
		# end with the new string
		i = len(old)
		new_sentence = sentence [:-i] + "{new}".format(new = new)
		return new_sentence

	# Return the original sentence if there is no match 
	return sentence
print(replace_ending("It's raining cats and cats", "cats", "dogs"))
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts"))
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april"))
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April"))
# Should display "The weather is nice in April"

And when I run it i get:

It's raining cats and dogs
She sells seashells by thedonuts
The weather is nice in May
The weather is nice in April

I am not understanding why the second line is wrong. I state "if new not in sentence" ( i tried if old in sentence before, but it didn't work for some reason)
but in that case, in which new is in sentence, it still changes it. Can someone help me understand what is wrong?
Thanks in advance

Attached Files

Thumbnail(s)
   
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