What is wrong with my code? | Sololearn: Learn to code for FREE!

+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.

5/28/2021 4:13:16 AM

Dianna

8 Answers

New Answer

+5

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))

+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.

+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))

+2

🅰🅹 🅐🅝🅐🅝🅣 Oh! That makes so much more sense now! Thank you I was so confused :)

+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.

+2

CHANDAN ROY Thank you! :)

+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.

0

fix = input() repl = input() print (text.count(fix)) print (text.replace(fix, repl))