+ 1

Can someone help me with "Editing Guide" problem of code coach?

Problem is as follows: Manipulating Strings 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. My Code text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." lst = text.split() find = input() replace = input() #print(lst) count = 0 for x in lst: if x == find: ind = lst.index(x) lst.pop(ind) lst.insert(ind,replace) count+=1 string = " " string = string.join(lst) print(count) print(string) It is not passing all the test cases. Please help

29th Mar 2021, 1:49 PM
Divyanshu Jaiswal
Divyanshu Jaiswal - avatar
11 Réponses
+ 8
The correct answer for this question is below!!✔✔✔👍👍👍👍✔✔✔ text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." sub_string1 = input() sub_string2 = input() print(text.count(sub_string1)) print(text.replace(sub_string1, sub_string2))
9th Aug 2021, 3:43 PM
ANDREW TSEGAYE
ANDREW TSEGAYE - avatar
+ 3
text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." lookingfor=input("") replacedchar=input("") countchar=text.count(lookingfor) print(countchar) newtxt=(text.replace(lookingfor,replacedchar)) print(newtxt) """this one worked for me"""
14th May 2021, 3:38 AM
molly
+ 1
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. Note, that you need to output the number of replacements, before the replaced text. Code text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." i=input() j=input() print(text.count(i)) newtext=text.replace(i,j) print(newtext)
4th Feb 2023, 9:03 PM
Guy Martial KEYOU
0
Ok if you split it using " " then (dollars,) and (dollars.) would not be compared see the string carefully again text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." lst = text.split() find = input() replace = input() #print(lst) count = 0 for x in lst: if find in x: ind = lst.index(x) lst.pop(ind) lst.insert(ind,replace) count+=1 string = " " string = string.join(lst) print(count) print(string) I think this will work for you.
29th Mar 2021, 2:35 PM
Ayush Kumar
Ayush Kumar - avatar
0
This code also works find = input() replace = input() print(text.count(find)) print(text.replace(find, replace))
28th May 2021, 4:03 AM
Abhishek Biswas
Abhishek Biswas - avatar
0
this is best answer: text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." sub_string1 = input() sub_string2 = input() print(text.count(sub_string1)) if sub_string1 in text: print(text.replace(sub_string1,sub_string2))
12th Aug 2021, 6:27 PM
Alisher Axmadov
Alisher Axmadov - avatar
0
text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." sub_string1 = input() sub_string2 = input() print(text.count(sub_string1)) if sub_string1 in text: print(text.replace(sub_string1,sub_string2))
12th Aug 2021, 6:28 PM
Alisher Axmadov
Alisher Axmadov - avatar
0
find = input() replace = input() print(text.count(find)) print(text.replace(find, replace))
20th Nov 2021, 12:43 PM
Stefan Bartl
Stefan Bartl - avatar
0
Getting four of five right Can not get the reverse driving me mad text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." x = input("") i = input("") print(text.count(x)) if x in text: print(text.replace(x,i))
31st Dec 2021, 10:30 AM
PJ Reilly
0
For those of you getting four out of five using string methods, please drop the if statement before the replace method. First of all it is reductant. Second of all, you need to print the text regardless if any changes were made. if you used regex to solve this please share your code, my test 5 is failing. ty
2nd Mar 2022, 11:02 PM
Areeb Qureshi
0
text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." i=input() j=input() print(text.count(i)) newtext=text.replace(i,j) print(newtext)
8th Jun 2022, 2:23 PM
Rahul Musale
Rahul Musale - avatar