+ 1
can you help me solve practice 5.2 editing guide in python data structures pls
12 odpowiedzi
+ 3
Lion Fret hi,
Did the solution provided by XXX worked for you.
I did something quite similar to your code and tried the XXX suggestion but it still doesn't work.
There are 5 tests case and the 4th one is failing. I can't check what's kind of error it is as the case stay locked/hidden.
I can't figure out what could go wrong as every input is of type string , this couldn't be a data type error, so what else could cause an error ?
Thanks for reading.
Anyway @XXX I'm not pro either and it seems I can Access at least one of the practice by course but as soon as I choose one to do the others appears locked...
+ 2
text = "I weigh 80 kilograms. He weighs 65 kilograms."
a1=input()
a2=input()
b=text.replace(a1,a2)
c=b.count(a2)
print(c)
print(b)#pls check my code
+ 2
Well, it works now.
Originally I added a
if wordToFind in text:
#the logic here
I removed it and reorder the count function as XXX suggested and it worked.(Though I made some tests in the playground and it worked whatever was the count on => wordToFind before the text replacement or replacer after text replacement but without the if statement).
So finally I guess the 4th test case was that the wordToFind input was not in the text and the count of occurence was 0.
Thanks.
+ 1
It should work now. Although, if it doesn't work, that would be because the replacement string is already present in the original string. The fix would be simply - put the line
c = text.count(a1)
before the 3rd line and remove the 4th line, that is
c = b.count.....
That way, you will be counting the number of occurences pf text to be replaced before the actual replacement takes place
0
That practise problem is for pro users only. Please copy the question here and also, post your attempt at solving it.
0
but i am pro users
0
Lion Fret I am not. And that is why, I can't see the question. That's why I am asking you to copy the question here and also post your attempt at solving it.
0
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 Outputu
2
I weigh 80 kg. He weighs 65 kg.
The program replaced 2 occurrences of the word kilograms with kg.
0
Lion Fret
you can use the .replace() method on strings
https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/methods/string/replace
0
thanks
0
For me this code worked:
text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer."
enter = input()
repl = input()
if enter in text:
new_text = text.replace(enter,repl)
print(new_text.count(repl))
print(new_text)
else:
print("0")
print(text)
0
Editing Guide
text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer."
substituted= input()
replace_text= input()
the_replacement= text.replace(substituted,replace_text)
print(text.count(substituted))
print(the_replacement)