Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the first output of this code "It's dogsing cats and cats" rather than the intended?

Question 5

The function replaces the old string in a sentence with the new string, only if the sentence ends with the old string. If more than one occurrence of the old string in sentence, only the one at the end is replaced. For example, replace_ending("abcabc", "abc", "xyz") should return abcxyz, not xyzxyz.

My code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def replace_ending(sentence, old, new):
    
   new_sentence=''
   new_sentence1=''
   if old in sentence:
      start_i = sentence.index(old) 
      oldword_length=len(old)
      new_sentence1=sentence[start_i+len(old):] 

      while old in new_sentence1:        
         start_i = new_sentence1.index(old)  
         oldword_length=len(old)
         new_sentence1 = new_sentence1[start_i+len(old):] 
        
   
      new_sentence=sentence[:start_i] + new + sentence[start_i+len(old):]  
      return new_sentence

  
   return sentence

    
print(replace_ending("It's raining cats and cats","cats","dogs")) 
# Should display "It's raining cats and dogs"

>Solution :

I removed your comments and replaced with comments showing you what is happening at each stage with the first input. This is something you can do yourself to diagnose issues with your code, or use a debugger to see the values of each variable at each stage and do the same thing.

def replace_ending(sentence, old, new):

    # sentence = "It's raining cats and cats"
    # old = "cats"
    # new = "dogs"

    new_sentence=''
    new_sentence1=''

    if old in sentence:
        # "Cats" is in "It's raining cats and cats", so this code executes

        start_i = sentence.index(old)
        # start_i = 13

        new_sentence1=sentence[start_i+len(old):]
        # start_i + len(old) = 13 + 4 = 17
        # new_sentence1 = sentence[17:] = "and cats"
            
        while old in new_sentence1:  
            # "cats" is in "and cats", so this code executes

            start_i = new_sentence1.index(old)
            # start_i = 4

            new_sentence1 = new_sentence1[start_i+len(old):]
            # new_sentence1 = new_sentence1[8:] = "" (empty string)
            # "Cats is not in the empty string so the while loop finishes here"
        
        new_sentence=sentence[:start_i] + new + sentence[start_i+len(old):]  
        # new_sentence = sentence[:4] + new + sentence[8:] = "It's " + "dogs" + "ing cats and cats"

        return new_sentence
        
    return sentence

I can kind of see what you are trying to do and where you went wrong, but this it feels like you are making it quite complicated. Research the built-in string method endswith as this might make it a lot easier.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading