What is wrong with my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is wrong with my code?

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. And I wrote the following code: https://code.sololearn.com/c3UJqXb0vAPR/?ref=app All the text cases were run successfully except for one that was hidden.

28th May 2021, 4:13 AM
Dianna
Dianna - avatar
9 Answers
+ 6
Dianna Reason :- For test case failing What if there is no f in text? Program will not print anything but you should print atleast 0 So no need to check condition, you can directly print results like this: f = input() r = input() print (text.count(f)) print (text.replace(f, r))
28th May 2021, 4:25 AM
A͢J
A͢J - avatar
+ 5
Dianna See my answer, I gave you reason. If you use if statement then it will bound you to do not print anything (if value not exist in text) but we should atleast print something.
28th May 2021, 4:33 AM
A͢J
A͢J - avatar
+ 2
text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." to_be_replaced = input() replacement = input() print (text.count(to_be_replaced)) print (text.replace(to_be_replaced, replacement))
28th May 2021, 4:27 AM
CHANDAN ROY
CHANDAN ROY - avatar
+ 2
Dianna when you use "if", it won't output anything when input value doesn't exist. But according to this challenge, your program should output zero in such cases.
28th May 2021, 4:38 AM
CHANDAN ROY
CHANDAN ROY - avatar
+ 2
CHANDAN ROY Thank you! :)
28th May 2021, 5:00 AM
Dianna
Dianna - avatar
+ 2
text="Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." input1 = str(input()) input2 = str(input()) a=text.count(input1) b=text.replace(input1, input2) print(a) print(b)
29th Oct 2022, 5:32 AM
Dave Kendrick Turmawan
Dave Kendrick Turmawan - avatar
+ 1
Thank you so much, this is much more simple than the code I wrote. But do you have any idea why using the if statement causes the test case to fail? I found it odd because the code seem to make sense.
28th May 2021, 4:30 AM
Dianna
Dianna - avatar
+ 1
🅰🅹 🅐🅝🅐🅝🅣 Oh! That makes so much more sense now! Thank you I was so confused :)
28th May 2021, 4:37 AM
Dianna
Dianna - avatar
+ 1
fix = input() repl = input() print (text.count(fix)) print (text.replace(fix, repl))
25th Feb 2022, 10:07 AM
Liria
Liria - avatar