+ 1

Why does that test caes 4 is not working properly??

text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." a = input() b = input() a = text.replace(a,b) print(a.count(b)) print(a) Here is the question: You are making a text editor and need to implement find/replace functionality. The code declares a text string. You need to take two string inputs, which represent the substring to find and what to replace it with in the text. Your program needs to output the number of replacements made, along with the new text. For example, for the given text "I weigh 80 kilograms. He weighs 65 kilograms.": Sample Input kilograms kg Sample Output 2 I weigh 80 kg. He weighs 65 kg. The program replaced 2 occurrences of the word kilograms with kg.

5th Jul 2021, 3:52 PM
Shahir
Shahir - avatar
1 ответ
+ 3
You need to count substring to be replaced BEFORE, replacement. And then replace them also better give variables meaningful names like: old new and, it'll be: print(text.count(old)) text= text.replace(old, new) print(text)
5th Jul 2021, 4:07 PM
Shadoff
Shadoff - avatar