为什么此代码的第一个输出是 "It's dogsing cats and cats" 而不是预期的?
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.
我的代码:
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"
我删除了您的评论,取而代之的是向您展示第一个输入的每个阶段发生了什么的评论。这是您可以自己诊断代码问题的方法,或者使用调试器查看每个阶段每个变量的值并执行相同的操作。
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
我能看出你在做什么,哪里出错了,但这感觉就像你把事情弄得很复杂。研究 built-in 字符串方法 endswith
,因为这可能会使它变得容易得多。
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.
我的代码:
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"
我删除了您的评论,取而代之的是向您展示第一个输入的每个阶段发生了什么的评论。这是您可以自己诊断代码问题的方法,或者使用调试器查看每个阶段每个变量的值并执行相同的操作。
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
我能看出你在做什么,哪里出错了,但这感觉就像你把事情弄得很复杂。研究 built-in 字符串方法 endswith
,因为这可能会使它变得容易得多。